获取gridview当前选中行的内容

来源:互联网 发布:和弓淘宝 编辑:程序博客网 时间:2024/05/21 04:40
方法一:SelectedIndex
获取gridview当前选中行的内容

txtid.Text = GridView1.Rows(GridView1.SelectedIndex).Cells(1).Text '获取第二列
txttitle.Text = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).Controls(0), HyperLink).Text '如果列是hyperlink控件,用此方法可以获取
txttime.Text = GridView1.Rows(GridView1.SelectedIndex).Cells(3).Text '获取第三列
txtauthor.Text = GridView1.Rows(GridView1.SelectedIndex).Cells(4).Text '获取第四列

方法二:SelectedRow

直接在GRIDVIEW里添加一列选择列,在SelectedIndexChanged事件下写 

TextBox.Text=GridView1.SelectedRow.Cells[你要显示在TextBox里的单元格的下标].Text; 

方法三:e.RowIndex

直接在GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)事件下写

GridView1.Rows[e.RowIndex].Cells[1].Controls[0]

方法四:NamingContainer

通过 ((GridViewRow)(chk.NamingContainer)).RowIndex 即可获得当前选择行的行号,行号有了之后就可以根据行号取任意列的数据了。

 问题关键所在是这里的CheckedChanged事件传入的参数sender为CheckBox,通过NamingContainer可以获取当前checkbox所在容器对象,即gridviewrow

    protected void chkrow_CheckedChanged(object sender, EventArgs e)

    {

        CheckBox chk = sender as CheckBox;

        int index = ((GridViewRow)(chk.NamingContainer)).RowIndex;

        if (chk.Checked)

        {

           this.lbmargemx.Text += this.gvordermx.Rows[index].Cells[1].Text.Trim() ;

        }

    }

方法五:DataKeys

GridView1.DataKeys[e.RowIndex]  //DataKeys获取主键那一列

GridView1.DataKeys[GridView1.Rows[row].RowIndex].Value

解释:DataKeys是什么主要看DataKeyNames="postId"是什么,即主要看select出来的主键是什么

0 0
原创粉丝点击