选中复选框时如何获取gridview行值? [英] how to get gridview row values when checkbox is checked?
问题描述
hi
如何在选中复选框时获取网格行值。我在按钮点击事件中使用此代码,但它不起作用..请你建议。
foreach (GridViewRow rw in grid1.Rows)
{
CheckBox chkBx =(CheckBox)rw.FindControl ( 检查);
if (chkBx!= null && chkBx.Checked)
{
lblEmail = grid1.Rows [ 0 ]。单元格[ 3 < /跨度>] Text.ToString();
}
}
提前感谢...
我认为只有一个修改可以找到你的解决方案。
试试这个:
string strEmail = stirng.Empty;
foreach (GridViewRow rw in grid1.Rows)
{
CheckBox chkBx =(CheckBox)rw.FindControl( Check);
if (chkBx!= null && chkBx.Checked)
{
strEmail =((Label)rw.FindControl( lblEmail))。 ;
// lblEmail是您要绑定EmailID的标签的ID。
// 此处strEmail已准备好您的电子邮件ID。在这里进行操作。
}
}
让我知道结果。
- Amit
引用:if(chkBx!= null&& chkBx.Checked)
{
不检查null并同时检查。如果对象为null,则将获得Null Reference异常。所以,尝试一次检查一个条件。
Quote:
lblEmail = grid1.Rows [0] .Cells [3] .Text.ToString();
每次你正在为同一个对象分配文本。如果lblEmail是标签,那么如何将文本分配给标签对象。
使用
< pre lang =c#> lblEmail.Text = grid1.Rows [ 0 ]。单元格[ 3 ] .Text.ToString();
因此,在每次迭代时,您都要分配给相同的标签/对象。你怎么能与别人区别开来。所以附上如下文字。
lblEmail.Text + =;+ grid1.Rows [0] .Cells [3]。 Text.ToString();
希望它有帮助。
在您可以使用的桌面应用程序中用于检查
是否选择了行使用C#。
if(dataGridView.SelectedRows.Count> -1)
{
//在这里写下你的代码。
}
else
{
//在此处编写代码。
}
hi
how to get grid row values when checkbox is checked. i am using this code in button click event, but it's not working.. could you please advice.
foreach (GridViewRow rw in grid1.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("Check");
if (chkBx != null && chkBx.Checked)
{
lblEmail = grid1.Rows[0].Cells[3].Text.ToString();
}
}
thanks in advance...
Hi,
As I think only one modification can find your solution.
Try this:
string strEmail=stirng.Empty; foreach (GridViewRow rw in grid1.Rows) { CheckBox chkBx = (CheckBox)rw.FindControl("Check"); if (chkBx != null && chkBx.Checked) { strEmail = ((Label)rw.FindControl("lblEmail")).Text; //lblEmail is ID of the label for which you are binding EmailID's. // here strEmail is ready with your email id. Do the operation here. } }
Let me know the results.
--Amit
Hi,
Quote:if (chkBx != null && chkBx.Checked)
{
do not check for null and checked at the same time. if the object is null, you will get Null Reference exception. SO, try to check one condition at a time.
Quote:
lblEmail = grid1.Rows[0].Cells[3].Text.ToString();
every time you are assigning text to same object. if "lblEmail" is label then how can you assign text to label object.
use
lblEmail.Text = grid1.Rows[0].Cells[3].Text.ToString();
so, on every iteration you are assigning to same label/object. how can you differentiate with others. so append the text like below.
lblEmail.Text += " ; " + grid1.Rows[0].Cells[3].Text.ToString();
hope it helps.
in Desktop Application You Can Use it for the checking
whether your row is selected or not Using C#.
if (dataGridView.SelectedRows.Count>-1)
{
//Write your Code here.
}
else
{
//Write your Code here.
}
这篇关于选中复选框时如何获取gridview行值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!