我现在想在GridView上点击某一行,然后获得这一行各字段的值,该怎么写?
有GridView.Row[]这样的吗?
有GridView.Row[]这样的吗?
解决方案 »
- c#中,部分方法为什么不能有返回值呢?
- C#中颜色值的转换——从Format32bppRgb到Format16bppRgb565,或Format16bppRgb565到Format24bppRgb
- 需要把一个没有源码的软件汉化。主要是:菜单,按钮,提示。
- DataGridView
- 不包含适合于入口点的静态“Main”方法
- WinForm程序中,在config文件中设置一个StringCollection的对象,如何在程序读取它呀
- 我快没着了。。大侠帮帮我
- 作WinForm程序,遇到一SQL语句,有关主从表(根据从表某一字段和,修改主表一字段值)
- C# 自己琢磨了一个开机自动启动程序 谁来帮我看看
- 问一个关于C#中struct的问题,希望耐心一点哦!!
- 如何写代码察看163邮箱的积分?
- C#Web页面中,有一个用户控件(里面有二个Button) 在一个页面A中调用它,如何在A中实现用户控件的事件功能啊
你可以通过DataGridView1.SelectRows[0]来访问第一行
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#FFC729',this.style.fontWeight='';"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
e.Row.Attributes.Add("ondblclick", "rekey('" + e.Row.Cells[1].Text + "','" + e.Row.Cells[10].Text + "')");
e.Row.Attributes["style"] = "Cursor:hand";
}
前台:
<script language="javascript" type="text/javascript">
function rekey(name,cdid)
{window.opener.document.getElementById("Text_PD_Name").value=name;
window.opener.document.getElementById("HiddenCDID").value=cdid
window.close();
}
</script>
dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
另一种是通过设置DataKeyNames属性为要获取值的列的名称,然后通DataKeys[index/name].Value/Values[index]得到数据的原值。
我写了:
DataKeyNames="pan"
GridView.DataKeys["pan"].Value,系统说不行,
有个简单的例子吗?
...
</asp:GridView>
protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
object objValue = this.grdBooks.DataKeys[e.Row.RowIndex].Value;
if (objValue != null)
{
//objValue为所取的值
}
}
<asp:GridView ID="grdView" runat="server" AutoGenerateColumns="False" DataKeyNames="fID" OnRowDataBound="grdView_RowDataBound">
...
</asp:GridView>
protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
object objValue = this.grdView.DataKeys[e.Row.RowIndex].Value;
//object objValue = this.grdView.DataKeys[e.Row.RowIndex].Values[0];
if (objValue != null)
{
//objValue为所取的值
}
}
DataKeyNames="A,B",
那我该怎么分别获取这两个字段的值?我写了如下:
grdView.DataKeys[0].Values;
grdView.DataKeys[1].Values;
系统说“索引超出范围。必须为非负值并小于集合大小。参数名: index”,
还是很糊涂....
grdView.DataKeys[rowIndex].Values[0];
grdView.DataKeys[rowIndex].Values[1];
grdView.DataKeys[rowIndex].Values[colName0];
grdView.DataKeys[rowIndex].Values[colName1];
grdView.DataKeys[rowIndex].Values[colName0];
grdView.DataKeys[rowIndex].Values[colName1];
grdView.DataKeys[rowIndex].Values[colName0];
grdView.DataKeys[rowIndex].Values[colName1];
这里的rowIndex就是你要获取哪一行的值,比如你现在选取第二行,它的值就应该是1(下标从0开始的),colName0,colName1
就是列的字段名,比如你有“name” “age”两个字段,那么colName0的值就应该是name
至于rowindex的值如何获得,就看你怎么实现了,最平常就是用gridview里面的select事件,可以获得此时的行号,也就是rowindex,不知道这样讲可明白