如何选择gridview中的特定行

时间:2010-03-10 09:58:30

标签: asp.net gridview

我有一个Gridview

<asp:GridView ID="GridView1" runat="server" Width="400px" AutoGenerateColumns="false"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name">
                    <ItemTemplate>
                        <asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="lblResidentialAddress" runat="server" Text='<%# Eval("ResidentialAddress") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>

我得到绑定到gridview的值

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {

        DataTable dt = new DataTable();
        dt = objdb.GetData("Getsamples", new object[] { });
        ViewState["CurrentTable"] = dt;
        GridView1.DataSource = dt;

        GridView1.DataBind();

    }
}

protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
    Response.Write(GridView1.SelectedRow.Cells[0].Text); 
  //  string selectedText = ((Label)GridView1.SelectedRow.FindControl("lblStudentName")).Text;

  //  Response.Write(selectedText);
}

我无法检索选中复选框的行...

如何选择gridview中的特定行,并根据选择我需要取出'Name'并将其作为参数传递给get,另一个gridview与我选择的行相关。?????? p>

任何帮助......

1 个答案:

答案 0 :(得分:0)

尝试使用其他活动 - OnSelectedIndexChanging http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanging.aspx

GridViewSelectEventArgs http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewselecteventargs_members.aspx)传递给具有 NewSelectedIndex 属性的事件处理程序。

您的事件处理程序将如下所示:

void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
     GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}