adding a row on a datagrid

R

rcoco

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.DataGridCommandEventArgs 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?
 
D

David Wier

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?
 
G

Guest

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
 
R

rcoco

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top