R
rcoco
Hi,
I want to share this problem. I have a datagrid that will help me
Insert data into sql database. So I made a button On my form so that
when I press the button a new row on datagrid should be created and I
could be able to insert data. But with this code below I've failed
could someone help me and tell me where I'm going wrong:
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Fill();
Bind();
}
}
DataTable table = new DataTable();
private void Fill()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from
dbo.DashBoard", con);
adapter.Fill(table);
}
private void Bind()
{
dgis.DataSource = table;
dgis.DataBind();
}
private void InsertEmpty()
{
table.Rows.InsertAt(table.NewRow(), 0);
}
private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;
// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Update";
// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// stop editing
dgis.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
break;
case "Update":
break;
case "Cancel":
break;
case "Edit":
// begin editing
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}
// fill and bind
Fill();
//InsertEmpty();
Bind();
}
Thanks.
I want to share this problem. I have a datagrid that will help me
Insert data into sql database. So I made a button On my form so that
when I press the button a new row on datagrid should be created and I
could be able to insert data. But with this code below I've failed
could someone help me and tell me where I'm going wrong:
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Fill();
Bind();
}
}
DataTable table = new DataTable();
private void Fill()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from
dbo.DashBoard", con);
adapter.Fill(table);
}
private void Bind()
{
dgis.DataSource = table;
dgis.DataBind();
}
private void InsertEmpty()
{
table.Rows.InsertAt(table.NewRow(), 0);
}
private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;
// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Update";
// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// stop editing
dgis.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
break;
case "Update":
break;
case "Cancel":
break;
case "Edit":
// begin editing
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[6];
ecc.UpdateText = "Insert";
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}
// fill and bind
Fill();
//InsertEmpty();
Bind();
}
Thanks.