如何在行数据绑定事件的网格视图中选中复选框。 [英] How to get the check box checked in a grid view on row data bound event.
本文介绍了如何在行数据绑定事件的网格视图中选中复选框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只有当价值为付费时,才应选中复选框,否则应取消选中。
我正在尝试此选项以检查所有复选框。
Only if value is "Paid" then only checkbox should be checked else it should unchecked.
I am trying this its making all the checkboxes checked.
protected void grdDataUpdate_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkFIPaid");
for (int i = 0; i < _dtStudentsFD.Rows.Count; i++)
{
if (_dtStudentsFD.Rows[i]["status"].ToString() == "Paid")
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
}
推荐答案
Why do you use another for loop in RowDataBOund... RowDatabound means it will be called for each and every row...
Solution 1:
Place anothe column in your data grid which takes the value of "Status" and make it invisible and then write ur code like this
protected void grdDataUpdate_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkFIPaid");
Label lblSatatus = (Label)e.Row.FindControl("lblStatusCol");
if (lblSatatus.Text.ToString() == "Paid")
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
这里当每一列被限制时访问状态值和复选框,然后根据该行的状态列值...将决定复选框状态。
如果它对您有用,请接受
protected void grdDataUpdate_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk =(CheckBox)e.Row.FindControl(chkFIPaid );
if(e.Row.Cells [i] .Text ==付费)
{
chk.Enabled =假;
}
其他
{
chk.Enabled = true;
}
}
}
protected void grdDataUpdate_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkFIPaid");
if (e.Row.Cells[i].Text == "Paid")
{
chk.Enabled = false;
}
else
{
chk.Enabled = true;
}
}
}
查看你的代码。我认为你做错了代码。
look at your code.i think your doing something wrong with fallowing code.
for (int i = 0; i < _dtStudentsFD.Rows.Count; i++)
{
if (_dtStudentsFD.Rows[i]["status"].ToString() == "Paid")
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
而不是上面的代码,你应该尝试使用fallowing方法..
这里,'e.Row.Cells [5] .Text'是状态字段的值
Instead of above code , you should try with fallowing approach..
Here,'e.Row.Cells[5].Text' is the value of status field
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk=(CheckBox)e.Row.cells[0].FindControl("chkMyCheckBox");
if(chk != null)
{
if(e.Row.Cells[5].Text == ""status"") //check for status column
chk.Checked = true;
}
}
}
这篇关于如何在行数据绑定事件的网格视图中选中复选框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文