Tuesday, May 27, 2014

How to Hide Column in DataTable or GridView

If you want to hide column in datatable dynamically, here is the simple code for that.

You need to add this code in load event or anywhere else.
// Create new Datatable
            DataTable dtStudent = new DataTable("Student");
           
            // Add Columns
            DataColumn DColStdID = new DataColumn("Stud_ID", System.Type.GetType("System.Int32"));

            // hide column in datatable. this will not display in DataGridView
            DColStdID.ColumnMapping = MappingType.Hidden;
            dtStudent.Columns.Add(DColStdID);


            DataColumn DColStdName = new DataColumn("Name", System.Type.GetType("System.String"));
            dtStudent.Columns.Add(DColStdName);


            DataColumn DColStdClass = new DataColumn("Class", System.Type.GetType("System.String"));
            dtStudent.Columns.Add(DColStdClass);
                                  

            //Adding data
            dtStudent.Rows.Add("100", "Wasi", "2nd");
            dtStudent.Rows.Add("101", "Shref", "3rd");

            dataGridView1.DataSource = dtStudent;

Now add the datagridview on form. And run the code.



No comments:

Post a Comment

Search This Blog