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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hardcode dataset and bind to grid

Status
Not open for further replies.

ISPrincess

Programmer
Feb 22, 2002
318
US
Suffice it to say I need to get some samples out in a design very quickly - stored procs are not ready and tables are not filled with sample data, so...

I need to quickly hardcode (evil word) a dataset and then bind it to a data grid.

I have some code that is giving me errors, so I was hoping that
A) either someone could provide me with another bit of sample code to accomplish this
or
B) take a look at the following code (code sample gathered from another thread on this site)
and tell me what I am doing wrong


I have a grid datagrid1 defined on my page
In the page_load event I call

Code:
Private dsData As DataSet
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
             InitializeDataSet()
             FillDataSet()
        end if
End sub


[code]
Public Sub InitializeDataset()
        Dim dt As DataTable

        dsData = New DataSet("ResourcePlanning")
        dt = dsData.Tables.Add("ResourcePlanning")

        dt.Columns.Add("Column1", GetType(SqlTypes.SqlString))
        dt.Columns.Add("Column2", GetType(SqlTypes.SqlString))
        dt.Columns.Add("Column3", GetType(SqlTypes.SqlInt16))
        dt.Columns.Add("Column4", GetType(SqlTypes.SqlString))
    End Sub

    Public Sub FillDataSet()
        Dim dt As DataTable
        Dim dr As DataRow
        dt = dsData.Tables(0)
        dr = dt.NewRow
        dr("VehicleType") = "Turret R"

        dr = dt.NewRow
        dr("Column1") = "Whatever"
        dr("Column2") = "TTR123"
        dr("Column3") = 1
        dr("Column4") = "Comments for TTR123"

        dr = dt.NewRow
        dr("Column1") = "Whatever2"
        dr("Column2") = "TTR890"
        dr("Column3") = 0
        dr("Column4") = "Comments for TTR890"

        DataGrid1.DataSource = dsData.Tables(0)
        DataGrid1.DataBind()

    End Sub

when I run the code, i get the error:


DataGrid with id 'Datagrid1' could not automatically generate any columns from the selected data source.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataGrid with id 'Datagrid1' could not automatically generate any columns from the selected data source.

Source Error:


Line 173:
Line 174: DataGrid1.DataSource = dsData.Tables(0)
Line 175: DataGrid1.DataBind()
Line 176:
Line 177: End Sub




Thank you very much !!

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Just by looking at it, the new row is not added to the table:
Code:
    Public Sub FillDataSet()
        Dim dt As DataTable
        Dim dr As DataRow
        dt = dsData.Tables(0)
        dr = dt.NewRow
        dr("VehicleType") = "Turret R"

        dr = dt.NewRow
        dr("Column1") = "Whatever"
        dr("Column2") = "TTR123"
        dr("Column3") = 1
        dr("Column4") = "Comments for TTR123"
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr("Column1") = "Whatever2"
        dr("Column2") = "TTR890"
        dr("Column3") = 0
        dr("Column4") = "Comments for TTR890"
        dt.Rows.Add(dr)

        DataGrid1.DataSource = dsData.Tables(0)
        DataGrid1.DataBind()

    End Sub
 
LV -
Thank you (again) for taking the time to point that out.

If I set my datagrid1 > AutoGenerateColumns="False"
I get no error but I get no grid

If I set my datagrid1 > AutoGenerateColumns="True"
I get the above mentioned error:
DataGrid with id 'Datagrid1' could not automatically generate any columns from the selected data source.

Why is that? What am I missing.

Thanks again for your help!


PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
I got it. Posted below for reference:


You need to set the AutoGenerateColumns="False"
then actually put this code in your <grid></grid>

Sample:

Code:
<Columns>

<asp:BoundColumn
HeaderText="Column1"
DataField="Column1"/>

<asp:BoundColumn
HeaderText="Column2"
DataField="Column2"/>

</Columns>


PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top