我想根据复选框点击插入特定的行值。设计和来源如下。从设计我必须选择一些项目。如果我单击批准,我必须将这些复选框选中的行值存储到DB中。帮我找一个合适的解决方案。
设计
ASPX:
C#:
以下给出的代码获取整个gridview值并存储到db中。如何根据上述要求修改此代码。
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);
}
}
答案 0 :(得分:3)
您需要遍历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
}
}
来源:http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx
答案 1 :(得分:1)
试试此代码
protected void btnClick_Click(object sender, EventArgs e)
{
StringBuilder sbQuery = new StringBuilder();
bool flag = false;
foreach (GridViewRow row in gridview1.Rows)
{
if (((CheckBox)row.FindControl("chk")).Checked)
{
flag = true;
}
}
}
我也建议您使用javascript
这是javascript我还有gridview的其他链接
http://forums.asp.net/t/1125079.aspx
如何在复选框OnCheckedChanged
时从gridview获取复选框值答案 2 :(得分:1)
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);
}
}
}