|
Odd datagrid/dataset behavior -- rhaazy --
SqlDataAdapter da = new SqlDataAdapter(datagridselect, str); DataSet ds = new DataSet("tblScanDetail"); da.Fill(ds); dataGrid1.DataSource = ds; datagridselect is determined by a function in my code.... For testing purposes I only have one available option for datagridselect to be...so I know exactly what the select statement is. I run the select statement in query analyzer and it works fine. However when i run it from this code it only displays one of the columns. The select statement is of the nature "select * from table" and there should be 6 columns that come back but only one does... Is there any explanation for this? |
|
-- Cerebrus --
Try viewing the content of your Dataset using the Ds.GetXml() method. It will tell you the schema of the Dataset that is being created. If it contains the columns that are not showing up in the Datagrid, then you can tell that the problem could be in the Datagrid configuration. If it doesn't contain those columns, then you know that the correct schema is not being generated. Try setting the MissingSchemaAction property explicitly (in case you changed it somewhere) to "Add". This is the default setting by the way. myDataAdapter.MissingSchemaAction = MissingSchemaAction.Add Let us know if this works, Regards, Cerebrus. |
|
-- rhaazy --
I am not familiar with how exactly to view the content using the getxml function.... However I did do a ds.table .columns.count and all the columns are there.. So I am guessing the problem lies in the datagrid, however I have no idea what would prevent it from displaying the rest of the columns, I have many other statements like the one posted above and they have no problem sending all the data to the grid. Perhaps my count method isn't giving me the answer I need, could you please explain how I can use this GetXml() method to view the dataset? |
|
-- rhaazy --
Alright I solved the problem, however I am not sure why this happened. In every other section of my code when I assign a dataset to a datagrid I followed with a helper function that sets the column width do fit the text within. void SizeDataGridColumnsToContent(DataGrid dataGrid, int nRowsToScan) { // Create graphics object for measuring widths. Graphics Graphics = dataGrid.CreateGraphics(); // Define new table style. DataGridTableStyle tableStyle = new DataGridTableStyle(); try { //DataTable dataTable = (DataTable)dataGrid.DataSource; DataTable dataTable = ((DataSet)dataGrid.DataSource).Tables ; //for the 1st table if (-1 == nRowsToScan) { nRowsToScan = dataTable.Rows.Count; } else { // Can only scan rows if they exist. nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count); } // Clear any existing table styles. dataGrid.TableStyles.Clear(); // Use mapping name that is defined in the data source. tableStyle.MappingName = dataTable.TableName; // Now create the column styles within the table style. DataGridTextBoxColumn columnStyle; int iWidth; for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++) { DataColumn dataColumn = dataTable.Columns ; columnStyle = new DataGridTextBoxColumn(); columnStyle.TextBox.Enabled = true; columnStyle.HeaderText = dataColumn.ColumnName; columnStyle.MappingName = dataColumn.ColumnName; // Set width to header text width. iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width); // Change width, if data width is wider than header text width. // Check the width of the data in the first X rows. DataRow dataRow; for (int iRow = 0; iRow < nRowsToScan; iRow++) { dataRow = dataTable.Rows ; if (null != dataRow ) { int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray .ToString(), dataGrid.Font).Width); iWidth = (int)System.Math.Max(iWidth, iColWidth); } } columnStyle.Width = iWidth + 4; // Add the new column style to the table style. tableStyle.GridColumnStyles.Add(columnStyle); } // Add the new table style to the data grid. dataGrid.TableStyles.Add(tableStyle); } catch(Exception e) { MessageBox.Show(e.Message); } finally { Graphics.Dispose(); } } I don't see why if I don't put the function after what I had that it causes that kind of behavior.... Please explain! |
|
-- rhaazy --
Ok I looked at the string returned by the ds.getxml() and all the information is there. So if this is true then what you're saying is that it would be a datagrid property problem... However this is confusing to me because everyother statement like the one that isn't working does work. No settings on the datagrid are changed anywhere in the code, only assignment of a dataset... |