k m

k m

  • NA
  • 2
  • 3.6k

update particular cel in datatable dynamically from textbox

Jan 4 2013 9:47 AM
public void BindData()
{
DataTable dt = new DataTable();
if (ViewState["datatab"] == null)
{
dt.Columns.Add("productid", typeof(string));
dt.Columns.Add("productname", typeof(string));
dt.Columns.Add("quantity", typeof(string));
DataRow dr = dt.NewRow();
//dr["productid"] = dt.Rows[0].ToString;
dr["productid"] = TextBox2.Text;
dr["productname"] = TextBox3.Text;
dr["quantity"] = TextBox4.Text;
dt.Rows.Add(dr);


}
else
{
dt = (DataTable)ViewState["datatab"];


//TextBox2.Text = GridView1.Rows[0].Cells[0].Text;
//TextBox3.Text = GridView1.Rows[0].Cells[1].Text;
//TextBox4.Text = GridView1.Rows[0].Cells[2].Text;


//int rowIndex = 0;
foreach (GridViewRow gvr in GridView1.Rows) //loop through GridView
{
//////quantity = Convert.ToInt32(TextBox4.Text);
//TextBox4.Text = quantity.ToString();
// DataRow dr = dt.NewRow();
////dr["productid"] = dt.Rows[0].ToString;
// dr["productid"] = gvr.Cells[0].Text;
//dr["productname"] =gvr.Cells[1].Text;
//dr["quantity"] = gvr.Cells[2].Text;
//dt.Rows.Add(dr);



//GridView1.DataSource = dt;
if (gvr.Cells[0].Text == TextBox2.Text & gvr.Cells[1].Text == TextBox3.Text)
{
ViewState["datatab"] = dt;
int quantity = 0;
//quantity = gvr.Cells[2].Text + TextBox4.Text ;
quantity = Convert.ToInt32(TextBox4.Text);
quantity = Convert.ToInt32(gvr.Cells[2].Text) + quantity;
////GridView1.DataSource = dt;
//dt.Rows.Add(TextBox2.Text, TextBox3.Text, TextBox4.Text);
//BindData();
//////dt.Rows[0]["quantity"] = quantity;
//gvr.Cells[2].Text = quantity.ToString();
gvr.Cells[2].Text = quantity.ToString();
//dt.Rows[2].ItemArray = new object[] { "2" };
//dt.Rows[0].ItemArray[0] = "5";

for (int i = 0; i < dt.Rows[i].ItemArray ; i++)
{
DataRow myRow = dt.Rows[i];
dt.Rows[i]["quantity"] = quantity;
for (int j = 0; j < dt.Columns[j].ColumnName; j++)
{
//quantity = Convert.ToInt32(gvr.Cells[2].Text) + quantity;
if (dt.Columns[j].ColumnName.IndexOf("quantity") > 0)
{

quantity = Convert.ToInt32(dt.Rows[i][2].ToString()) + quantity;
} //quantity = Convert.ToInt32(TextBox4.Text);
//quantity = Convert.ToInt32(gvr.Cells[2].Text) + quantity;
//quantity = Convert.ToInt32(dt.Rows[i][2].ToString()) + quantity;
//quantity = Convert.ToInt32(dt.Rows[i][j].ToString());
//dt.Rows[i][2] = quantity;
//dt.Rows.Add(TextBox2.Text, TextBox3.Text, gvr.Cells[2].Text);
//quantity= Convert.ToInt32(gvr.Cells[i].Text);
//dt.ColumnChanged += columnIndex.;
// //dt.Rows.Add(gvr.Cells[2].Text);
//dt.AcceptChanges();
//dt.Rows[i].SetField(column, value);
//dt.Rows[i].SetField(2, value);
//dt.Rows[i].SetField(quantity , value);
ViewState["datatab"] = quantity.ToString();
//ViewState["datatab"] = dt;
//GridView1.DataBind();
//GridView1.DataSource = dt;


//GridView1.DataBind();
}
}
}

else
{
if (gvr.Cells[0].Text == TextBox2.Text & gvr.Cells[1].Text == TextBox3.Text)
{
ViewState["datatab"] = dt;
}
else
{


}

}

}
//dt.Rows.Add(TextBox2.Text, TextBox3.Text, TextBox4.Text);
// if (( (TextBox1.Text) = dt.Columns[0].ColumnName.IndexOf("productid")) && ( (TextBox2.Text)=dt.Columns[1].ColumnName.IndexOf("productname") ))
// {

}
// }
// else
// {
// }


//if (this.GridView1.RowCount == i)
// break;
//}

for (int i = 0; i < dt.Rows.Count; i++)
{

if ((TextBox2.Text ==dt.Rows[i][0].ToString() ) & (TextBox3.Text == dt.Rows[i][1].ToString()))
{

}
else
{
dt.Rows.Add(TextBox2.Text, TextBox3.Text, TextBox4.Text);

}
}
ViewState["datatab"] = dt;

GridView1.DataSource = dt;


GridView1.DataBind();



//GridView1.DataSource = dt;


//GridView1.DataBind();

}