I have a datasource which could have a different number of columns every time it is run. I've tried to use the RowDataBound event to set the size of each column based on it's title (as below).
The problem is, if there are a lot of columns, the widths shrink. Is there a way to enforce this width regardless of how many columns I have?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int j = 0; j < NoOfColumns; j++)
{
string row = e.Row.Cells[j].Text;
if (row.Contains("Date"))
{
e.Row.Cells[j].Width = new Unit(150);
}
else if (row.Contains("Transaction"))
{
e.Row.Cells[j].Width = new Unit(250);
}
else if (row.Contains("+"))
{
e.Row.Cells[j].Width = new Unit(300);
}
else if (row.Contains("Balance"))
{
e.Row.Cells[j].Width = new Unit(400);
}
}
}