Helen Trim wrote:
> How can I change the column width in a gridview filled with a dataset? I
> can't get TableStyle to work. Has anyone any sample code, please?
>
> Thanks,
> Helen
>
> PS. I am creating a web page in ASP.Net 2.0 using C#.
>
> This is my code so far:
>
> SqlConnection cn; // Connection to the SQL database
> SqlDataAdapter da;
> DataSet ds;
>
> cn = new SqlConnection();
> da=new SqlDataAdapter("Select * From Patient",cn);
> ds = new DataSet();
>
> OpenConnection(cn); // Connects to the database
> da.Fill(ds,"PatientTable");
> dgPatients.DataSource = ds.Tables["PatientTable"];
> dgPatients.DataBind();
Hi,
Add event handler for the RowCreated event:
.... OnRowCreated="GridView1_RowCreated" ...
protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
DataControlFieldCell cell =
(DataControlFieldCell)e.Row.Cells[i];
//here add custom logic of column width settings, for example:
//if (cell.ContainingField.HeaderText == "Id")
// e.Row.Cells[i].Width = new Unit("100px");
}
}
}
> cn.Close();
|