469,602 Members | 1,838 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

adding a row on a datagrid

Hi,
I'm trying to Insert a new Row on a dagrid. When I did a google
search, I got an example on this address: http://www.codeproject.com/ASPNET_DataGrid.asp.
I've done mycode as follows:

private void Fill()
{
DataSet ds=new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter("select * from DashBoard",
con);
adapter.Fill(ds);
}
private void Bind()
{
DataSet ds=new DataSet();
dgis.DataSource = ds;
dgis.DataBind();
}
private void InsertEmpty()
{
DataSet table=new DataSet();
Table.Rows.InsertAt(Table.NewRow(), 0);
}
private void bttnew_Click(object sender, System.EventArgs e)
{

dgis.EditItemIndex = 0;

EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Insert";

Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
dgis.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
break;

case "Edit":
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

Fill();
Bind();
}

On compiling I get this error

-'System.Web.UI.WebControls.Table' does not contain a definition for
'NewRow'
-'System.Web.UI.WebControls.TableRowCollection' does not contain a
definition for 'InsertAt'
-'An object reference is required for the nonstatic field, method, or
property 'System.Web.UI.WebControls.Table.Rows'

Where could I be going wrong?

Feb 26 '07 #1
3 2256
try adding a row to the TABLE itself - not the dataset
Dim dr As DataRow = DataTable.NewRow()
DataTable.Rows.InsertAt(dr, 0)

Then, rebind the table to the datagrid

btw - which version of DotNet are you using?
--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

"rcoco" <nc******@yahoo.cawrote in message
news:11**********************@z35g2000cwz.googlegr oups.com...
Hi,
I'm trying to Insert a new Row on a dagrid. When I did a google
search, I got an example on this address:
http://www.codeproject.com/ASPNET_DataGrid.asp.
I've done mycode as follows:

private void Fill()
{
DataSet ds=new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter("select * from DashBoard",
con);
adapter.Fill(ds);
}
private void Bind()
{
DataSet ds=new DataSet();
dgis.DataSource = ds;
dgis.DataBind();
}
private void InsertEmpty()
{
DataSet table=new DataSet();
Table.Rows.InsertAt(Table.NewRow(), 0);
}
private void bttnew_Click(object sender, System.EventArgs e)
{

dgis.EditItemIndex = 0;

EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Insert";

Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
dgis.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
break;

case "Edit":
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

Fill();
Bind();
}

On compiling I get this error

-'System.Web.UI.WebControls.Table' does not contain a definition for
'NewRow'
-'System.Web.UI.WebControls.TableRowCollection' does not contain a
definition for 'InsertAt'
-'An object reference is required for the nonstatic field, method, or
property 'System.Web.UI.WebControls.Table.Rows'

Where could I be going wrong?

Feb 26 '07 #2
Are you sure you got this exact code from somebody's article?

private void InsertEmpty()
{
DataSet table=new DataSet();
Table.Rows.InsertAt(Table.NewRow(), 0);
}

What the above does is (as David pointed out) attempt to Insert a Row on a
DataSet, which has no "Rows" collection. Your first line should look like:
DataTable table = new DataTable();

the DataTable class DOES have a Rows collection.

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"rcoco" wrote:
Hi,
I'm trying to Insert a new Row on a dagrid. When I did a google
search, I got an example on this address: http://www.codeproject.com/ASPNET_DataGrid.asp.
I've done mycode as follows:

private void Fill()
{
DataSet ds=new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter("select * from DashBoard",
con);
adapter.Fill(ds);
}
private void Bind()
{
DataSet ds=new DataSet();
dgis.DataSource = ds;
dgis.DataBind();
}
private void InsertEmpty()
{
DataSet table=new DataSet();
Table.Rows.InsertAt(Table.NewRow(), 0);
}
private void bttnew_Click(object sender, System.EventArgs e)
{

dgis.EditItemIndex = 0;

EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Insert";

Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
dgis.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
break;

case "Edit":
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

Fill();
Bind();
}

On compiling I get this error

-'System.Web.UI.WebControls.Table' does not contain a definition for
'NewRow'
-'System.Web.UI.WebControls.TableRowCollection' does not contain a
definition for 'InsertAt'
-'An object reference is required for the nonstatic field, method, or
property 'System.Web.UI.WebControls.Table.Rows'

Where could I be going wrong?

Feb 26 '07 #3
Thanks all,
That was actually my problem. This is what I was supposed to do and it
worked
private void InsertEmpty()
{
DataTable table = new DataTable();
table.Rows.InsertAt(table.NewRow(), 0);
}

But I got another problem When I run my form, I'm only able to see the
header of the datagrid and cannot see the containt of the table. Could
some one be knowing why? Thanks.

Feb 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Clayton Hamilton | last post: by
5 posts views Thread by Phil Townsend | last post: by
3 posts views Thread by Jim Heavey | last post: by
2 posts views Thread by Bob Hollness | last post: by
3 posts views Thread by Fao, Sean | last post: by
2 posts views Thread by Flack | last post: by
1 post views Thread by Tor Inge Rislaa | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.