点击datagrid行的事件是什么,如何点击上面一行,把这行数据显示到textbox中?

zzxap 2008-07-11 11:17:53
点击datagrid行的事件是什么,如何点击上面一行,把这行数据显示到textbox中?
gridview获取包含当前单元格的行的属性是CurrentRow。
datagrid获取包含当前单元格的行的属性是什么?

private void DataGrid1_Click(object sender, EventArgs e//这里的响应要修改)
{
this.Textbox1.Text = this.DataGrid1.CurrentRow.Cells[0].Value.ToString();
this.TextBox2.Text = this.DataGrid1.CurrentRow.Cells[1].Value.ToString();
this.Textbox3.Text = this.DataGrid1.CurrentRow.Cells[2].Value.ToString();
//或者这样?this.Textbox3.Text textbox1.text=datagrid1.selecteditem.cells[1].text;


}
这个不适用于datagrid。因为CurrentRow不是DataGrid的属性。



...全文
587 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
lfywy 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 evjen 的回复:]
看我的代码:保证行,我用的是2005里面的DataGrid控件,代码里面只需要更改相应的事件明就行了

C# codeprotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
for (i = -1; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row…
[/Quote]
安宁 2008-07-11
  • 打赏
  • 举报
回复
TO: zzxap

谢谢提醒.. 我顺手粘贴的... 没注意..
zzxap 2008-07-11
  • 打赏
  • 举报
回复
我只要知道datagrid点击行要用什么时间即可。以及要引入什么命名空间?
zzxap 2008-07-11
  • 打赏
  • 举报
回复
gridview点击行用cellclick
gridview点击行用什么事件,以及要引入什么命名空间?
xlong224 2008-07-11
  • 打赏
  • 举报
回复
rowCommand
zzxap 2008-07-11
  • 打赏
  • 举报
回复
DataGridRowEventArgs要引入什么命名空间?
zzxap 2008-07-11
  • 打赏
  • 举报
回复
RowEventArgs类型在vs2003与vs2005有着一些区别
GridViewRowEventArgs在VS2005下为e.row
DataGridRowEventArgs在VS2003为为e.item

2003的DataGrid在OnDataBound中可以捕获生成行的事件,而在
2005的Gridview在OnRowDataBound事件中才可以捕获
zzxap 2008-07-11
  • 打赏
  • 举报
回复
protected void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.Textbox1.Text =this.DataGrid1.SelectedItem.Cells[1].Text ;
this.TextBox2.Text =this.DataGrid1.SelectedItem.Cells[2].Text ;

//this.Textbox3.Text = this.DataGrid1.SelectedItem.Cells[2].Text ;
}
evjen 2008-07-11
  • 打赏
  • 举报
回复
怎么不行啊,一样的道理啊
zzxap 2008-07-11
  • 打赏
  • 举报
回复
还是不行。。
zzxap 2008-07-11
  • 打赏
  • 举报
回复
 DataGrid完全是一个Table结构的控件,DataGrid包含DataGridItem,每个DataGridItem又包含TableCell,那么,我们就可以在TableCell的某个控件中,利用控件的Parent来得到TableCell,再利用TableCell的Parent,就可以得到DataGridItem了。   
protected void ddl_SelectedIndexChanged(object sender, System.EventArgs e) //一定要声明成protected或者public,不能是private的。
{
DropDownList ddl = (DropDownList)sender;
TableCell cell = (TableCell)ddl.Parent;
DataGridItem item = (DataGridItem)cell.Parent;
Response.Write(item.Cells[0].Text);
}
zzxap 2008-07-11
  • 打赏
  • 举报
回复
唉。datagrid和gridview是不同的。参考不了
evjen 2008-07-11
  • 打赏
  • 举报
回复
在datagrid里面是ItemDataBound事件
if (e.Item.RowType == DataControlRowType.DataItem)
{
e.Item.Attributes.Add("onclick", "rekey('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text.Replace(" ", "") + "'");
e.Item.Attributes["style"] = "Cursor:hand";
}

zzxap 2008-07-11
  • 打赏
  • 举报
回复
to xiaoqijun
'System.Web.UI.WebControls.TableCell' does not contain a definition for 'Value'
icelau 2008-07-11
  • 打赏
  • 举报
回复
我把在GridView 中的代码给你参考下!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "selectKey(" + GridView1.DataKeys[e.Row.RowIndex]["id"]+");");
}
}

<script type="text/javascript">
function selectKey(key,key2)
{
.....
}
</script>
大体上是这样,我做个点击一行,下边显示该行相关信息到另一个GridView 中,但愿对你有帮助!
evjen 2008-07-11
  • 打赏
  • 举报
回复
看我的代码:保证行,我用的是2005里面的DataGrid控件,代码里面只需要更改相应的事件明就行了
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
for (i = -1; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "rekey('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text.Replace(" ", "") + "'");
e.Row.Attributes["style"] = "Cursor:hand";
}
}
}


js代码:
 function rekey(num,name) 
{
document.getElementById("txtNum").value=num;
document.getElementById("txtEName").value=name ;
}
JustLovePro 2008-07-11
  • 打赏
  • 举报
回复
datagrid没有用过,用的datagridview应该差不多》
可以在cellclick事件中获得:

if (e.RowIndex > -1)
{
string no = dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
}
安宁 2008-07-11
  • 打赏
  • 举报
回复

this.Textbox1.Text = this.DataGrid1.Items[this.DataGrid1.SelectedIndex].Cells[0].Value.ToString();
this.Textbox2.Text = this.DataGrid1.Items[this.DataGrid1.SelectedIndex].Cells[1].Value.ToString();
this.Textbox3.Text = this.DataGrid1.Items[this.DataGrid1.SelectedIndex].Cells[2].Value.ToString();
zzxap 2008-07-11
  • 打赏
  • 举报
回复
private void DataGrid1_Click(object sender, EventArgs e)
{
this.Textbox1.Text = this.DataGrid1.SelectedItem.Cells[2].Text;
this.TextBox2.Text =this.DataGrid1.SelectedItem.Cells[2].Text ;
this.Textbox3.Text = this.DataGrid1.SelectedItem.Cells[2].Text ;
}
这样也不行,还是没有触发点击事件
brooklyng60 2008-07-11
  • 打赏
  • 举报
回复
用e.items啊,把e.rows换成items
加载更多回复(4)

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧