簡體   English   中英

按鈕單擊行時,在C#Gridview中獲取單元格值

[英]Get Cell Value in C# Gridview when Button clicked on row

我有一個gridviewMain。 每當我單擊第1行的CLOSE linkbutton時,我想獲得值aa。 單擊第2行上的關閉獲取bb的值。 有任何想法嗎。

A    B   C  
aa  xx   3  CLOSE 
bb  yy   4  CLOSE
cc  zz   5  CLOSE

ASPX

 <asp:BoundField DataField="afield" HeaderText="A" 
     SortExpression="afield" >
    <ItemStyle HorizontalAlign="Center" />
 </asp:BoundField>
 <asp:BoundField DataField="bfield" HeaderText="B" SortExpression="cfield" >
     <ItemStyle HorizontalAlign="Center" />
  </asp:BoundField>
  <asp:BoundField DataField="cfield" HeaderText="C" SortExpression="cfield" >
     <ItemStyle HorizontalAlign="Center" />
   </asp:BoundField>
 <asp:TemplateField ShowHeader="False">

       <ItemTemplate>
          <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False"  CommandName="CloseClicked" Text="Close"></asp:LinkButton>

        </ItemTemplate>

                    </asp:TemplateField>

假設您正在使用BoundFields作為前三列,並且您想要處理LinkButton -click事件(而不是GridView RowCommand ):

protected void CloseLinkClicked(Object sender, EventArgs e)
{
    var closeLink = (Control) sender;
    GridViewRow row = (GridViewRow) closeLink.NamingContainer;
    string firstCellText = row.Cells[0].Text; // here we are
}

如果您正在使用TemplateFields並且值aa在Label中(例如LblValue ):

Label lblValue = (Label) row.FindControl("LblValue"); // lblValue.Text = "aa"

使用數據鍵。 更簡單:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeValue, AnotherValue" ... >

然后在代碼隱藏中,只需獲取與該行關聯的鍵:

var rowIndex = 0;
var someValue = GridView1.DataKeys[rowIndex]["SomeValue"] as string;

您需要將CommandName分配給GridView標記中的LinkButton列。 從那里你還需要連接OnRowCommand事件來處理你的Close命令。

以下是在GridView上使用Add命令的示例:

Markup:
<asp:gridview id="ContactsGridView" datasourceid="ContactsSource" allowpaging="true" 
    autogeneratecolumns="false" onrowcommand="ContactsGridView_RowCommand" runat="server">
    <columns>
       <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
       <asp:boundfield datafield="ContactID" headertext="Contact ID"/>
       <asp:boundfield datafield="FirstName" headertext="First Name"/> 
       <asp:boundfield datafield="LastName" headertext="Last Name"/>
    </columns>
</asp:gridview>

Code-Behind:
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection.
        GridViewRow row = ContactsGridView.Rows[index];

        // Create a new ListItem object for the contact in the row.     
        ListItem item = new ListItem();
        item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
        Server.HtmlDecode(row.Cells[3].Text);

        // If the contact is not already in the ListBox, add the ListItem 
        // object to the Items collection of the ListBox control. 
        if (!ContactsListBox.Items.Contains(item))
        {
            ContactsListBox.Items.Add(item);
        }
    }
}

你可以這樣做

<asp:GridView ID="grid" runat="server" 
AutoGenerateColumns="false" onrowcommand="grid_RowCommand" >
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%#Eval("xxx")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %> 
CommandName="arg">Click</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

 protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowindex = int.Parse(e.CommandArgument.ToString());
((TextBox)(grid.Rows[rowindex].FindControl("txtgvunit"))).Text
} 

參考http://dotnetinbox.blogspot.in/2014/02/get-gridview-row-data-in-c.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM