如何从栅格视图中获取复选框选定值?c#
我想根据复选框单击插入特定的行值。设计和来源如下所示。我必须从设计中选择一些项目。如果单击“批准”,则必须将这些复选框选中的行值存储到数据库中。帮我找到一个合适的解决办法 设计: ASPX: C#: 下面给出的代码获取整个gridview值并存储到db中。如何根据上述要求修改此代码如何从栅格视图中获取复选框选定值?c#,c#,asp.net,gridview,checkbox,C#,Asp.net,Gridview,Checkbox,我想根据复选框单击插入特定的行值。设计和来源如下所示。我必须从设计中选择一些项目。如果单击“批准”,则必须将这些复选框选中的行值存储到数据库中。帮我找到一个合适的解决办法 设计: ASPX: C#: 下面给出的代码获取整个gridview值并存储到db中。如何根据上述要求修改此代码 protected void btnApprove_Click(object sender, EventArgs e) { ShadingAnalysisDataSetTableAdapt
protected void btnApprove_Click(object sender, EventArgs e)
{
ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter rs;
rs = new ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter();
foreach (GridViewRow row in GridView2.Rows)
{
string ItemName = row.Cells[0].Text;
string Quantity = row.Cells[1].Text;
rs.testInsert(ItemName, Quantity);
}
}
您需要遍历gridview并在当前行的单元格中按Id查找复选框
foreach (GridViewRow row in GridView2.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
bool chk = chkRow.Checked;
// Do your stuff
}
}
源代码:下面给出的代码工作正常。
按钮点击后的C#在哪里?没有人想为所选项目和每个项目的数据库查询编写完整的检查。@grovesNL:我添加了c#代码。@Vipin:您的问题解决了吗?@Imad:是的。现在它运行良好。
foreach (GridViewRow row in GridView2.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
if (chkRow.Checked)
{
string ItemName = row.Cells[0].Text;
string Quantity = row.Cells[1].Text;
rs.testInsert(ItemName, Quantity);
}
}
}