从gridview获取所选行的数据?

时间:2012-10-12 06:31:31

标签: asp.net

我是asp.net的新手。我正在做一个项目。因为我使用了一个gridview并使用sqldatasource从数据库中获取数据。 gridview代码是

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="Library" CellPadding="4" ForeColor="#333333" 
    GridLines="None" OnRowDataBound="GridView1_RowDataBound" >
    <RowStyle BackColor="#EFF3FB" />
    <Columns>
        <asp:BoundField DataField="Sl_No" HeaderText="Sl_No" SortExpression="Sl_No" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Author" HeaderText="Author" 
            SortExpression="Author" />
        <asp:BoundField DataField="Publication" HeaderText="Publication" 
            SortExpression="Publication" />
        <asp:BoundField DataField="Available" HeaderText="Status" 
            SortExpression="Available" />
        <asp:TemplateField HeaderText="Availability">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Available") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="RIUserTaken" HeaderText="RIUserTaken" 
            SortExpression="RIUserTaken" Visible="False" />
        <asp:TemplateField HeaderText="Taken By" ShowHeader="False">
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("RIUserTaken", "{0}") %>'></asp:Label>
                <asp:Button ID="SendRequest" runat="server" Text="Button" Visible="False" 
                    onclick="SendRequest_Click" CommandName="SendRequestCmd" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Taken Date" InsertVisible="False" 
            ShowHeader="False">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("TakenDate") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" /> </asp:GridView>

代码是网格视图的列之一。如果我点击该按钮,我将存储该按钮包含行字符串变量中的所有数据。喜欢 string slno =“”; slno = gridview1.cells [0]的.text; (我不确定它是否正确) 任何人都帮帮我??

提前感谢:)

2 个答案:

答案 0 :(得分:1)

从msdn,您应该检查一下。 GridView.SelectedIndexChanged event

void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}

void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this 
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];

// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with 
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{

  e.Cancel = true;
  MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";

}
}

答案 1 :(得分:0)

做这些事

步骤1.将RowCommand Evnt连接到您的GridView
 第2步。在代码隐藏的事件中,执行此操作

if(e.CommandName.Equals("SendRequestCmd"))
{
    var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow;
    // now access the cells like this
    var clickedSLNo = clickedRow.cells[0].Text;
}

<小时/> 更新:

e.CommandSource定义

  

表示命令源的System.Object类的实例。

<小时/> IButtonControl.CommandArgument定义

  

实现IButtonControl接口的控件必须实现CommandArgument属性和CommandName属性,以指示传播到Command事件的参数和命令名称。