Hi
How to hide GridView column if Autogenerate columns = true. Below is my code. But i want to read its value
C#
SQLUtility sQLUtility = new SQLUtility();
DataTable dt = sQLUtility.GeBookPlanningRecommendation(vRpl, BookCollection, StudentCollection).Tables[0];
if (dt != null)
{
//DataColumn Remarks = new DataColumn("Remarks", typeof(string));
//dt.Columns.Add(Remarks);
DataColumn Dates = new DataColumn("Date", typeof(DateTime));
dt.Columns.Add(Dates);
grdPlanning.DataSource = dt;
grdPlanning.DataBind();
}Thanks

Tuhin PaulRank #7 · 71.9K reputationPosted May 3, 2023, 2:29 PM EDT
To hide a specific column in a GridView control when AutoGenerateColumns property is set to true, you can modify your existing code slightly to add an extra condition before binding the DataSource.
Tuhin PaulRank #7 · 71.9K reputationPosted May 3, 2023, 2:30 PM EDT
If you want to hide a specific column in a GridView with AutoGenerateColumns set to true, you can use the DataBound event of the GridView to loop through the columns and set the Visible property to false for the column you want to hide.
See the code:
protected void grdPlanning_DataBound(object sender, EventArgs e) { // Set the column index of the column you want to hide int columnIndexToHide = 3; // replace with the actual column index you want to hide // Hide the column if (grdPlanning.HeaderRow != null && grdPlanning.HeaderRow.Cells.Count > columnIndexToHide) { grdPlanning.HeaderRow.Cells[columnIndexToHide].Visible = false; } foreach (GridViewRow row in grdPlanning.Rows) { if (row.RowType == DataControlRowType.DataRow) { if (row.Cells.Count > columnIndexToHide) { row.Cells[columnIndexToHide].Visible = false; } } } }In this example, the column index to hide is set to 3, but you can replace it with the actual column index you want to hide. The code loops through each row of the GridView and sets the Visible property of the cell in the specified column index to false. The same is done for the header row to hide the column header. You can add this event handler method to your code behind file and attach it to the DataBound event of the GridView control by adding the following attribute to the GridView tag in your markup:
OnDataBound="grdPlanning_DataBound"This will call the grdPlanning_DataBound method every time the GridView is databound and hide the specified column.