如何在c#中从单元格中获取值
本文关键字:单元格 获取 | 更新日期: 2023-09-27 18:15:29
我有一个表格,每行包含一个按钮。如果单击该按钮,我希望从该特定行的第一列检索值。这是我的代码,我怎么能这么做。谢谢你
foreach (var c in list)
{
TableRow row = new TableRow();
TableCell c0 = new TableCell();
c0.Text = string.Format("<img src='"+c.Movie_poster+ "'/>");
TableCell c1 = new TableCell();
c1.Text = c.Name;
TableCell c2 = new TableCell();
c2.Text = c.Date.ToStirng();
TableCell c3 = new TableCell();
c3.Text = c.desc;
TableCell c4 = new TableCell();
c4.Text = c.genre;
TableCell c5 = new TableCell();
//Add each string to cell in row
row.Cells.Add(c0);
row.Cells.Add(c1);
row.Cells.Add(c2);
row.Cells.Add(c3);
row.Cells.Add(c4);
row.Cells.Add(c5);
//Add the row to the table
Table.Rows.Add(row);
Button getname = new Button();
getname.Text = "Reserve";
getname.Click+= new EventHandler(getname_Click);
c5.Controls.Add(getname);
}
}
protected void reserve_Click(object sender,EventArgs e)
{
Button btn = (Button)sender;
TableRow row = (TableRow)btn.
string name = row.Cells[1].Text;
lbl.Text = name;
}
在创建按钮之前,应该写一个带有行号的新属性。
//Add the row to the table
Table.Rows.Add(row);
Button getname = new Button();
getname.Attributes.Add("idRow", idrow);
idrow += 1;
getname.Text = "Reserve";
getname.Click += new EventHandler(getname_Click);
然后在你的reserve_Click中,你应该得到的行ID存储在按钮属性
Button btn = (Button)sender;
int idRow = int.Parse(btn.Attributes["id"]);
TableRow row = t.Rows[idRow];
您可以将Column的值作为CommandArgument
绑定到Button。
Button getname = new Button();
getname.Text = "Reserve";
getname.CommandName = "Reserve";
getname.CommandArgument = "Column Value";
getname.Command += new CommandEventHandler(Button1_Command);
然后在按钮命令
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Reserve")
{
string columnValue = e.CommandArgument.ToString();
}
}