Hi
I have below code in Gridview RowDataBound. I want to set font-size
C#
TableCell cell = new TableCell();
TextBox txtColumn = new TextBox();
txtColumn.ID = "txtSessionDate";
txtColumn.CssClass = "form-control daterange-single";
cell.Controls.Add(txtColumn);
e.Row.Cells.Add(cell);Thanks

Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:43 AM EDT
See below my observations:
1. Use the var keyword to simplify the code and improve readability:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var txtColumn = (TextBox)e.Row.Cells[0].Controls[0]; txtColumn.CssClass = "form-control daterange-single"; txtColumn.Font.Size = new FontUnit("12px"); } }2. Use a more descriptive variable name instead of `txtColumn`, such as `textBox`.
3. Consider moving the CSS class assignment outside the RowDataBound event handler if it is a static value for all TextBox controls in the GridView. This way, it will not be set repeatedly for each row during the event.
// Set the CSS class for TextBox controls in the GridView (outside the event handler) protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var txtColumn = (TextBox)e.Row.Cells[0].Controls[0]; txtColumn.CssClass = "form-control daterange-single"; } } // Set the font size for TextBox controls in the GridView (inside the event handler) protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var textBox = (TextBox)e.Row.Cells[0].Controls[0]; textBox.Font.Size = new FontUnit("12px"); } }Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 21, 2023, 8:05 AM EDT
Hi Ramco to do that kindly modify your code as below
.gridview-textbox { font-size: 14px; /* Set the desired font size here */ }TableCell cell = new TableCell(); TextBox txtColumn = new TextBox(); txtColumn.ID = "txtSessionDate"; txtColumn.CssClass = "form-control daterange-single gridview-textbox"; // Add the gridview-textbox class here cell.Controls.Add(txtColumn); e.Row.Cells.Add(cell);Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:21 AM EDT
You can set the font size of the text box in the RowDataBound event by using the following code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TextBox txtColumn = (TextBox)e.Row.Cells[0].Controls[0]; txtColumn.CssClass = "form-control daterange-single"; txtColumn.FontSize = "12px"; } }