我已动态创建数据表绑定到网格视图。每个行都添加按钮单击事件。现在按钮单击事件我需要确定数据表中行的位置,并在该按钮单击上用另一行替换行值。在这一行我是changign行值,但是如何告诉他哪一行应该放这个值dtCurrentTable.Rows [0] [“Artikal”] = Label12.Text; ?下面是我的代码。帮助!
//创建行的方法
private void AddNewRecordRowToGrid()
{
if (ViewState["Markici"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
HttpCookie cookie = Request.Cookies["Democookie"];
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = 222;//Request.Cookies["kasa"].Value;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label3.Text;
drCurrentRow["Cena1"] = Label4.Text;
drCurrentRow["Kolicina"] = Label5.Text;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label6.Text;
drCurrentRow["VkDanok"] = Label8.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label23.Text;
drCurrentRow["EdMera"] = Label10.Text;
drCurrentRow["ArtikalID"] = Label9.Text;
drCurrentRow["TarifaID"] = Label7.Text;
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Added New Record to the DataTable
dtCurrentTable.Rows.InsertAt(drCurrentRow, 0);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}
//按钮点击事件,这里是创建行的调用方法
protected void Button6_Click(object sender, EventArgs e)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
if (Label37.Text == "0")
{
dtCurrentTable.Rows[0]["Kolicina"] = Label5.Text;
}
if (Label37.Text != "0")
{
Counter = Counter + 1;
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + Counter;
}
//Here I'm replaceing row values, but I need to determine row index,to know on wich row I;m changing values.
dtCurrentTable.Rows[0]["Artikal"] = Label3.Text;
dtCurrentTable.Rows[0]["Cena1"] = Label4.Text;
dtCurrentTable.Rows[0]["VkIznos"] = Label6.Text;
dtCurrentTable.Rows[0]["VkDanok"] = Label8.Text;
dtCurrentTable.Rows[0]["EdMera"] = Label10.Text;
dtCurrentTable.Rows[0]["ArtikalID"] = Label9.Text;
dtCurrentTable.Rows[0]["Vid"] = Label23.Text;
dtCurrentTable.Rows[0]["TarifaID"] = Label7.Text;
dtCurrentTable.AcceptChanges();
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
RemoveDuplicates(dt);
}
答案 0 :(得分:0)
尝试使用Select()
获取所需的行,并Rows.IndexOf()
查找行的索引。
If (dtCurrentTable.Select("Artikal='" + Label3.Text + "'").Length > 0) {
int rowIndex = dtCurrentTable.Rows.IndexOf(dtCurrentTable.Select("Artikal='" + Label3.Text + "'")[0]);
}
有关Select()
方法的详细信息,请参阅https://msdn.microsoft.com/en-us/library/h71xaeh0(v=vs.110).aspx。