By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

Getting Values from an Updatable data grid

P: n/a
Hi,
May be this is silly, or I just need a second pair eyes to look at this.

I am trying to get values that I have edited in a datagrid and update the
values with those values. Here is the code that I am using in my update
command.

TextBox txtProdName = (TextBox)e.Item.Cells[1].Controls[0];
TextBox txtSuppID = (TextBox)e.Item.Cells[2].Controls[0];
TextBox txtCatID = (TextBox)e.Item.Cells[3].Controls[0];
TextBox txtQtyPerUnit = (TextBox)e.Item.Cells[4].Controls[0];
TextBox txtUnitPrice = (TextBox)e.Item.Cells[5].Controls[1];
TextBox txtUnitsInStock = (TextBox)e.Item.Cells[6].Controls[0];
TextBox txtUnitsOnOrder = (TextBox)e.Item.Cells[7].Controls[0];
TextBox txtReorderLevel = (TextBox)e.Item.Cells[8].Controls[0];
CheckBox ChkDiscontinued = (CheckBox)e.Item.Cells[9].Controls[1];

string strSQL;

strSQL = "UPDATE Products SET ";
strSQL += "ProductName = '" + txtProdName.Text.Replace("'","'''") + "',";
strSQL += "SupplierID = " + txtSuppID.Text + "," ;
strSQL += "CategoryID = " + txtCatID.Text + ",";
strSQL += "QuantityPerUnit = '" + txtQtyPerUnit.Text + "',";
strSQL += "UnitPrice = " + txtUnitPrice.Text + ",";
strSQL += "UnitsInStock = " + txtUnitsInStock.Text + ",";
strSQL += "UnitsOnOrder = " + txtUnitsOnOrder.Text + ",";
strSQL += "ReorderLevel = " + txtReorderLevel.Text + ",";
strSQL += "Discontinued = " +
Convert.ToInt32(Convert.ToBoolean(ChkDiscontinued. Checked));
strSQL += " WHERE ProductID = " + e.Item.Cells[0].Text;

I am only getting values that I already have before I edited the cells. What
Am I doing wrong here.
--
Thanks
Pinjala.
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Try:

if(!this.IsPostback)
{
datagrid.DataSource = dataObject;
datagrid.DataBind();
}

Otherwise, once posting back, editing values are replaced by values from
data source.

HTH

Elton Wang
"Kiran Kumar Pinjala" wrote:
Hi,
May be this is silly, or I just need a second pair eyes to look at this.

I am trying to get values that I have edited in a datagrid and update the
values with those values. Here is the code that I am using in my update
command.

TextBox txtProdName = (TextBox)e.Item.Cells[1].Controls[0];
TextBox txtSuppID = (TextBox)e.Item.Cells[2].Controls[0];
TextBox txtCatID = (TextBox)e.Item.Cells[3].Controls[0];
TextBox txtQtyPerUnit = (TextBox)e.Item.Cells[4].Controls[0];
TextBox txtUnitPrice = (TextBox)e.Item.Cells[5].Controls[1];
TextBox txtUnitsInStock = (TextBox)e.Item.Cells[6].Controls[0];
TextBox txtUnitsOnOrder = (TextBox)e.Item.Cells[7].Controls[0];
TextBox txtReorderLevel = (TextBox)e.Item.Cells[8].Controls[0];
CheckBox ChkDiscontinued = (CheckBox)e.Item.Cells[9].Controls[1];

string strSQL;

strSQL = "UPDATE Products SET ";
strSQL += "ProductName = '" + txtProdName.Text.Replace("'","'''") + "',";
strSQL += "SupplierID = " + txtSuppID.Text + "," ;
strSQL += "CategoryID = " + txtCatID.Text + ",";
strSQL += "QuantityPerUnit = '" + txtQtyPerUnit.Text + "',";
strSQL += "UnitPrice = " + txtUnitPrice.Text + ",";
strSQL += "UnitsInStock = " + txtUnitsInStock.Text + ",";
strSQL += "UnitsOnOrder = " + txtUnitsOnOrder.Text + ",";
strSQL += "ReorderLevel = " + txtReorderLevel.Text + ",";
strSQL += "Discontinued = " +
Convert.ToInt32(Convert.ToBoolean(ChkDiscontinued. Checked));
strSQL += " WHERE ProductID = " + e.Item.Cells[0].Text;

I am only getting values that I already have before I edited the cells. What
Am I doing wrong here.
--
Thanks
Pinjala.

Nov 19 '05 #2

P: n/a
Thanks, This worked.
--
Thanks
Kiran Kumar Pinjala
"Elton W" wrote:
Try:

if(!this.IsPostback)
{
datagrid.DataSource = dataObject;
datagrid.DataBind();
}

Otherwise, once posting back, editing values are replaced by values from
data source.

HTH

Elton Wang
"Kiran Kumar Pinjala" wrote:
Hi,
May be this is silly, or I just need a second pair eyes to look at this.

I am trying to get values that I have edited in a datagrid and update the
values with those values. Here is the code that I am using in my update
command.

TextBox txtProdName = (TextBox)e.Item.Cells[1].Controls[0];
TextBox txtSuppID = (TextBox)e.Item.Cells[2].Controls[0];
TextBox txtCatID = (TextBox)e.Item.Cells[3].Controls[0];
TextBox txtQtyPerUnit = (TextBox)e.Item.Cells[4].Controls[0];
TextBox txtUnitPrice = (TextBox)e.Item.Cells[5].Controls[1];
TextBox txtUnitsInStock = (TextBox)e.Item.Cells[6].Controls[0];
TextBox txtUnitsOnOrder = (TextBox)e.Item.Cells[7].Controls[0];
TextBox txtReorderLevel = (TextBox)e.Item.Cells[8].Controls[0];
CheckBox ChkDiscontinued = (CheckBox)e.Item.Cells[9].Controls[1];

string strSQL;

strSQL = "UPDATE Products SET ";
strSQL += "ProductName = '" + txtProdName.Text.Replace("'","'''") + "',";
strSQL += "SupplierID = " + txtSuppID.Text + "," ;
strSQL += "CategoryID = " + txtCatID.Text + ",";
strSQL += "QuantityPerUnit = '" + txtQtyPerUnit.Text + "',";
strSQL += "UnitPrice = " + txtUnitPrice.Text + ",";
strSQL += "UnitsInStock = " + txtUnitsInStock.Text + ",";
strSQL += "UnitsOnOrder = " + txtUnitsOnOrder.Text + ",";
strSQL += "ReorderLevel = " + txtReorderLevel.Text + ",";
strSQL += "Discontinued = " +
Convert.ToInt32(Convert.ToBoolean(ChkDiscontinued. Checked));
strSQL += " WHERE ProductID = " + e.Item.Cells[0].Text;

I am only getting values that I already have before I edited the cells. What
Am I doing wrong here.
--
Thanks
Pinjala.

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.