Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Could not automatically generate any columns....."

Status
Not open for further replies.

NipsMG

Programmer
Dec 27, 2000
215
US
I've got a dataset that I've generated automatically through code (i.e. created the tables, columns myself), and I'm trying to bind it to a web datagrid.

In WinForms, this goes off without a hitch, either by supplying the dataset as the source or the datatable in the dataset.

However, in a web datagrid, I get this error:
Code:
DataGrid with id 'dgData' could not automatically generate any columns from the selected data source

This exception fires on dgData.DataBind().

Setting the "AutoGenerateColumns" property in the asp:DataGrid tag to false just causes NOTHING to come up at all.

Anyone know why this is happening? Should I just write a script to manually create the column names?

Thanks in advance.

--NipsMG s-)
 
All the datagrid cloumns ID's must match the DB field names exactly.
do yours?
 
You can programatically bind DataTableColumns to a DataGrid.
Basically, you can iterare through the columns collection of the DataTable and for each column, create a Bound Column on the datagrid, with the source as the DataTable column. I usually use this for dynamically created DataGrids.

Have you manually bound the DatGrids columns in the form designer? If have sirjon is right in what he says, as the bound fields may not match up at run time when you bind the datasource. If you have done this, clear the bindings you've created, re-bind them and try it again.



Rhys
Thought out... Maybe,
Opinionated... Probably
But it is only an opinion!
 
I haven't explicitly defined any bould columns in the asp:datagrid tag.. here's what I did:

Code:
<asp:DataGrid Runat=&quot;server&quot; ID=&quot;dgData&quot; BackColor=#eeeeee&quot; Width=&quot;85%&quot; HorizontalAlign=&quot;Center&quot;
Font-Name=&quot;Verdana&quot; CellPadding=&quot;4&quot; Font-Size=&quot;10pt&quot; 
AutoGenerateColumns=&quot;True&quot;>
<HeaderStyle BackColor=&quot;#000000&quot; ForeColor=&quot;White&quot; Font-Bold=&quot;True&quot; HorizontalAlign=&quot;Center&quot; />
<AlternatingItemStyle BackColor=&quot;White&quot; />
</asp:DataGrid>


'**** THIS IS IN my .vb CodeBehind file.
Public Class BlahBlahBlah

    Private dsData as DataSet

    Public Sub New()
        InitializeDataset()
    End Sub

    Public Sub InitializeDataset()
        Dim dt as DataTable
        dsData = new DataSet(&quot;FinalData&quot;)
        dt = dsData.Tables.Add(&quot;FinalData&quot;)
        dt.Columns.Add(&quot;SalesOrder, GetType(SqlTypes.SqlInt32)&quot;)
        dt.Columns.Add(&quot;t_orno&quot;, GetType(SqlTypes, SqlInt32))
        dt.Columns.Add(&quot;t_pono&quot;, GetType(SqlTypes, SqlInt16))
        dt.Columns.Add(&quot;t_item&quot;, GetType(SqlTypes, SqlString))
        dt.Columns.Add(&quot;t_qana&quot;, GetType(SqlTypes, SqlDouble))
        dt.Columns.Add(&quot;t_date&quot;, GetType(SqlTypes, SqlDateTime))
        dt.Columns.Add(&quot;Level&quot;, GetType(SqlTypes, SqlSingle))
    End Sub

    Public Sub Process()
        'FILL DATASET
        'THERE IS VAlID CODE HERE THAT FILLS THE DATASET
        'VIA THE FOLLOWING METHOD:
        dim dt as DataTable
        Dim dr as DataRow
        dt = dsData.Tables(0)
        dr = dt.NewRow
        dr(&quot;FieldName&quot;) = Value
        dt.Rows.Add dr


        'FINISH FILLING DATASET BLAH BLAH
        dgData.DataSource = dsData.Tables(0)
        dgData.DataBind() '***ERrOR THROWN HERE***
    End Sub 
   
    'More extraneous private mehtods go here...

End Class

Do I need to explicitly define each column in the dataset as a bound column in the asp tags beforehand?

--NipsMG


 
Have you set the Datagrid's datasource in the Web Forms designer?

Do you really need to use the SQL Data Types?

What's the purpose of the grid, simple display, display and editing etc. as this could affect best approach...

Rhys
Thought out... Maybe,
Opinionated... Probably
But it is only an opinion!
 
No i haven't set the datagrid's datasource in the designer. I didn't think this was necessary, as the dataset is not present until runtime.

No, I don't need to use the Sql Data Types, just used to using sqlClient classes, what else should I use.

The grid is simply for display.. I could auto-gen html tables through code, which is what i was doing before.. I was trying to use the DataGrid to make life easier.. apparently that didn't work :)

--NipsMG
 
You don't need to set the DataSource in the designer, I was just curious. If it's all simply for display, you don't really need to define your DataTable either. What I tend to do is create a Typed dataset on the page I want the data to appear on, then dynamically add a named datatable to that, dynamically create a DataAdaptor to fill the table, dynamically create and format my datagrid then add it to a panel control on the page.
Starting with a blank page, (or page with titles etc), add a panel, placed where you want it.

in the page behind I'm doing the below, (this may be a bit long winded so bear with me...)
<CODE>
// Clear DataTables from DataSet
// dsReports is my Typed DataSet
dsReports.Tables.Clear();

// Create new, undefined, DataTable in the Reports DataSet
dsReports.Tables.Add(&quot;UsrRpt_AbsoluteCreditExposure&quot;);

// Instantiate a data adapter object to fill the data table
SqlDataAdapter daSQL = new SqlDataAdapter(sSQL,cnSQLLogin);

// Fill data table
daSQL.Fill(dsReports.Tables[&quot;UsrRpt_AbsoluteCreditExposure&quot;]);

// Instantiate DataGrid object to display report
DataGrid dg = new DataGrid();

// Set it's data source
dg.DataSource = dsReports.Tables[&quot;UsrRpt_AbsoluteCreditExposure&quot;];

// Bind the grid to the data source
dg.DataBind();

// No auto generated colums or there is no colums in the
// colums collection
dg.AutoGenerateColumns = false;

// Manually bind Data Table columns to Data Grid columns
foreach (DataColumn c in dsReports.Tables[&quot;UsrRpt_AbsoluteCreditExposure&quot;].Columns)
{
BoundColumn bc = new BoundColumn();
bc.HeaderText = c.ColumnName;
dg.Columns.Add(bc);
}

// Format data grid
// Headers
dg.HeaderStyle.BackColor = System.Drawing.Color.AliceBlue;
dg.HeaderStyle.Font.Name = &quot;Arial&quot;;
dg.HeaderStyle.Font.Size = 10;
dg.HeaderStyle.Font.Bold = true;
dg.HeaderStyle.Wrap = false;
dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dg.HeaderStyle.VerticalAlign = VerticalAlign.Middle;
dg.ID = &quot;ReportDataGrid&quot;;
dg.Height = 51;

// Do not allow word wrapping on items...
dg.ItemStyle.Wrap = false;

Panel1.Wrap = false;
Panel1.Controls.Add(dg);
}
</CODE>

This is just how I do it, but it does work, and I'm sure you can play with the methodology to get what you want...

Hope it helps.

Rhys
Thought out... Maybe,
Opinionated... Probably
But it is only an opinion!
 
I tried dynamically adding bound columns, however, it did not work for some reason.

Adding the bound columns statically using asp:BoundColumn in the columns section of the asp:DataGrid tag worked perfectly.. However, following your example, adding the bound columns dynamically left me with a blank page.

I called DataBind() before dynamically adding the columns as in your example, and tried DataBind() after, and I got the same result.. When using yoru headerstyle commands, I got a blank table, but only after databinding AFTER everything in the example.

Any ideas why?

--NipsMG s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top