Reputation: 20598
I am new ASP.NET and I have never used a GridView or DataGrid, but I cannot find helpful examples online on how to do what I need. I need some data to be displayed on a page and I know that I should use a GridView, but I cannot seem to get anything to display.
I have the following DataSet:
DataSet ds = new DataSet("MyTables");
ds.Tables.Add("Users");
ds.Tables["Users"].Columns.Add("ID");
ds.Tables["Users"].Columns.Add("Name");
ds.Tables["Users"].Columns.Add("Email");
ds.Tables["Users"].Rows.Add(0,"Ace","[email protected]");
ds.Tables["Users"].Rows.Add(1,"Biff","[email protected]");
ds.Tables["Users"].Rows.Add(2,"Chuck","[email protected]");
ds.Tables["Users"].Rows.Add(3,"Dick","[email protected]");
myGrid.DataSource = ds.Tables["Users"].DefaultView;
myGrid.DataBind();
Heres is my ASP.NET:
<asp:GridView ID="myGrid" runat="server">
</asp:GridView>
Upvotes: 1
Views: 9505
Reputation: 103740
I think you're adding columns to the wrong table. Try something like this:
DataSet ds = new DataSet("MyTables");
ds.Tables.Add("Users");
DataTable userTable = ds.Tables["Users"];
userTable.Columns.Add("ID");
userTable.Columns.Add("Name");
userTable.Columns.Add("Email");
userTable.Rows.Add(0,"Ace","[email protected])";
userTable.Rows.Add(1,"Biff","[email protected])";
userTable.Rows.Add(2,"Chuck","[email protected])";
userTable.Rows.Add(2,"Dick","[email protected])";
myGrid.DataSource = userTable;
myGrid.DataBind();
Upvotes: 3
Reputation: 6325
I'm thinking that you're getting a object null reference error. What's happening is that you're creating columns in a table called 'add users' where there is no table called 'add users'. If you just change the three lines that you have ds.Tables["Add Users"] and change that to just ds.Tables["Users"] everything should work out just fine.
Hope this works for you.
Upvotes: 0
Reputation: 14280
You are referencing two completely different tables. You create the Table USERS then add columns to a table called ADD USERS then add rows to the Users table. Nothing will show up when you bind this.
Change this code
ds.Tables["Add Users"].Columns.Add("ID");
ds.Tables["Add Users"].Columns.Add("Name");
ds.Tables["Add Users"].Columns.Add("Email");
to this
ds.Tables["Users"].Columns.Add("ID");
ds.Tables["Users"].Columns.Add("Name");
ds.Tables["Users"].Columns.Add("Email");
Upvotes: 0