Hi
I have below code in Gridview RowDataBound. I want to give header also the name like Date.
C#
TableCell cell = new TableCell();
TextBox txtColumn = new TextBox();
txtColumn.ID = "txtSessionDate";
txtColumn.CssClass = "form-control daterange-single";
txtColumn.Font.Size = new FontUnit("10px");
cell.Controls.Add(txtColumn);
e.Row.Cells.Add(cell);Thanks

Deepak RawatRank #646 · 3.7K reputationPosted Jun 9, 2023, 1:13 AM EDT
To add a header to the
TableCellin theGridView'sRowDataBoundevent, you can create a newTableCellfor the header, set its text, and add it to theGridView's header row. Here's an updated version of your code:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { TableCell headerCell = new TableCell(); headerCell.Text = "Date"; // Set the header text e.Row.Cells.Add(headerCell); } else if (e.Row.RowType == DataControlRowType.DataRow) { TableCell cell = new TableCell(); TextBox txtColumn = new TextBox(); txtColumn.ID = "txtSessionDate"; txtColumn.CssClass = "form-control daterange-single"; txtColumn.Font.Size = new FontUnit("10px"); cell.Controls.Add(txtColumn); e.Row.Cells.Add(cell); } }In this code, I've added a condition to check if the current row is the header row (
DataControlRowType.Header). If it is, a newTableCellis created, and the desired header text is set using theTextproperty. Finally, the header cell is added to theGridView's header row (e.Row.Cells.Add(headerCell)).Make sure to replace
GridView1with the ID of your actualGridViewcontrol in your code.Amit MohantyRank #112 · 20K reputationPosted Jun 9, 2023, 1:20 AM EDT
TableCell headerCell = new TableCell(); headerCell.Text = "Date"; e.Row.Cells.Add(headerCell); TableCell cell = new TableCell(); TextBox txtColumn = new TextBox(); txtColumn.ID = "txtSessionDate"; txtColumn.CssClass = "form-control daterange-single"; txtColumn.Font.Size = new FontUnit("10px"); cell.Controls.Add(txtColumn); e.Row.Cells.Add(cell);Ramco RamcoRank #462 · 5.4K reputationPosted Jun 9, 2023, 2:14 AM EDT
Hi All
When i write Header part it shows columns like below . Secondly i want this column to be non-sortable
Image attached . In the end see column is going outside
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.Header) { TableCell headerCell = new TableCell(); headerCell.Text = "Date"; // Set the header text headerCell.ForeColor = System.Drawing.Color.Blue; e.Row.Cells.Add(headerCell); //e.Row.Cells[dtCount].Text = "Session Date"; } if (e.Row.RowType != DataControlRowType.DataRow) return; e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center; int columnIndex = dtCount - 1; TableCell cell = new TableCell(); TextBox txtColumn = new TextBox(); txtColumn.ID = "txtSessionDate"; txtColumn.CssClass = "form-control daterange-single"; txtColumn.Font.Size = new FontUnit("10px"); cell.Controls.Add(txtColumn); e.Row.Cells.Add(cell);Thanks