Setting Colum widths in a datagrid
-
I am programatically building a dataset that populates a datagrid. I need to then specfify the column widths for each column. However, the grid control ignores my input. I first tried setting the widths under TableStyles/GridColumnStyles. That was ignored so I figured it was because I wasn't populating the grid from a predefined datasource. So then I tried progammatically. Still nothing. Below is my code. The grid should only have one table in it. The grid populates correctly, it just doesn't format the widths of the columns. The dataset only returns 3 columns.:confused: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true;
-
I am programatically building a dataset that populates a datagrid. I need to then specfify the column widths for each column. However, the grid control ignores my input. I first tried setting the widths under TableStyles/GridColumnStyles. That was ignored so I figured it was because I wasn't populating the grid from a predefined datasource. So then I tried progammatically. Still nothing. Below is my code. The grid should only have one table in it. The grid populates correctly, it just doesn't format the widths of the columns. The dataset only returns 3 columns.:confused: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true;
I am assuming that its a winform application: Peter8675309 wrote: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); dgResults.TableStyles.Clear(); DataTableStyle tabStyle1=new DataTableStyle(); tabStyle1.MappingName = //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; dgResults.TableStyles.Add(tabStyle1); //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true; try this :
//get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); //populate the grid dgResults.DataSource = dsResults.Tables[0]; <<-- maybe!!! //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true;
"Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
this.Dispose();
"A Bug is a piece ofcode
that knows whatz its purpose" -
I am assuming that its a winform application: Peter8675309 wrote: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); dgResults.TableStyles.Clear(); DataTableStyle tabStyle1=new DataTableStyle(); tabStyle1.MappingName = //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; dgResults.TableStyles.Add(tabStyle1); //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true; try this :
//get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); //populate the grid dgResults.DataSource = dsResults.Tables[0]; <<-- maybe!!! //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true;
"Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
this.Dispose();
"A Bug is a piece ofcode
that knows whatz its purpose"Yes this is a windows form. If I add in dgResults.TableStyles[0].GridColumnStyles.Clear(); I get an 'index out of range' error unless I prepopulate the TableStyles of the grid prior. I took a look at the code at section 5.3.2 from http://www.codeproject.com/csharp/PracticalGuideDataGrids4.asp#\_Toc56951053. This functions (if I set the dt equal to me dataset.Column), but it still doesn't modify the column widths. X| Thanks for your quick reply.
-
Yes this is a windows form. If I add in dgResults.TableStyles[0].GridColumnStyles.Clear(); I get an 'index out of range' error unless I prepopulate the TableStyles of the grid prior. I took a look at the code at section 5.3.2 from http://www.codeproject.com/csharp/PracticalGuideDataGrids4.asp#\_Toc56951053. This functions (if I set the dt equal to me dataset.Column), but it still doesn't modify the column widths. X| Thanks for your quick reply.
Peter8675309 wrote: dgResults.TableStyles[0].GridColumnStyles.Clear(); clear the table styles, not the gridcolumnstyles. Well it seems to work for me:
this.dataGrid1.DataSource=null;
this.dataGrid1.TableStyles.Clear();DataTableStyles tab1=new DataTableStyles();
tab1.MappingName="ShoppingTables";this.dataGrid1.TableStyles.Add(tab1);
//Sets the data source of the grid to the data table returned
this.dataGrid1.DataSource= DbHelper.GetDataSet(blah...blah).Tables[0];
this.dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 50;
this.dataGrid1.TableStyles[0].GridColumnStyles[1].Width = 0; //hide columnthis.dataGrid1.AllowNavigation = false;
this.dataGrid1.AllowSorting = false;if( this.dataGrid1.VisibleRowCount > 0)
this.dataGrid1.Select(0);Let me know if this works for you, in my case i am making the width of of 1st column to 50 and 2nd column hidden. "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
this.Dispose();
"A Bug is a piece ofcode
that knows whatz its purpose" -
Peter8675309 wrote: dgResults.TableStyles[0].GridColumnStyles.Clear(); clear the table styles, not the gridcolumnstyles. Well it seems to work for me:
this.dataGrid1.DataSource=null;
this.dataGrid1.TableStyles.Clear();DataTableStyles tab1=new DataTableStyles();
tab1.MappingName="ShoppingTables";this.dataGrid1.TableStyles.Add(tab1);
//Sets the data source of the grid to the data table returned
this.dataGrid1.DataSource= DbHelper.GetDataSet(blah...blah).Tables[0];
this.dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 50;
this.dataGrid1.TableStyles[0].GridColumnStyles[1].Width = 0; //hide columnthis.dataGrid1.AllowNavigation = false;
this.dataGrid1.AllowSorting = false;if( this.dataGrid1.VisibleRowCount > 0)
this.dataGrid1.Select(0);Let me know if this works for you, in my case i am making the width of of 1st column to 50 and 2nd column hidden. "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
this.Dispose();
"A Bug is a piece ofcode
that knows whatz its purpose"Where is datatablestyles defined? I don't see it in the help of on MS's site. How is this class defined? If it comes from WebControls, I am working on a windows form. Thanks for your help.
-
Where is datatablestyles defined? I don't see it in the help of on MS's site. How is this class defined? If it comes from WebControls, I am working on a windows form. Thanks for your help.
Peter8675309 wrote: datatablestyles I sincerely apologize for misleading you by datatablestyles. its actually DataGridTableStyles. Its part of the System.Windows.Forms namespace. Hope this helps, again i am sorry for the wrong class name fiasco!:((:-O "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
this.Dispose();
"A Bug is a piece ofcode
that knows whatz its purpose" -
Peter8675309 wrote: datatablestyles I sincerely apologize for misleading you by datatablestyles. its actually DataGridTableStyles. Its part of the System.Windows.Forms namespace. Hope this helps, again i am sorry for the wrong class name fiasco!:((:-O "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
this.Dispose();
"A Bug is a piece ofcode
that knows whatz its purpose"