Get GridView Row

本文关键字:Row GridView Get | 更新日期: 2023-09-27 18:06:04

我有一个GridView,我绑定到Page_Load上的SqlDataReader。它有一个带有按钮的列,我试图在使用以下代码单击按钮时获得行:

int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];

EDIT:从注释区粘贴.aspx页面

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" OnRowCommand="GridView1_RowCommand" DataKeyNames="id" GridLines="None"> <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
        <asp:TemplateField> 
             <ItemTemplate> 
                  <asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' /> 
             </ItemTemplate> 
         </asp:TemplateField> 
     </Columns>
</aspx:GridView> 

我得到以下错误:'System. 'FormatException:输入字符串格式不正确。' on line 'int index = Convert.ToInt32(e. commandparameter);'。

任何想法?

Get GridView Row

您需要检查GridView行中哪个命令已被单击。您的标记应该相应地映射。见下面的鸡蛋。你得到的e.CommandArgument可能与你点击的按钮不对应。

在后台代码:

    void GridView1_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 = CustomersGridView.Rows[index];
    // additional logic...
    }
    // additional logic...
}
在标记:

也请确保你已经适当地设置了你的CommandArgument属性。在下面的例子:

<asp:Button (...) CommandArgument="<%# Container.DataItemIndex %>" />

或者使用按钮字段

<asp:ButtonField ButtonType="button" CommandName="Add" Text="Add" />

能否贴出整个标记代码,这将有助于解决。根据你的问题在gridview aspx代码中,您必须使用命令名称和命令参数按钮控件,它应该绑定到db的列之一。并使用gridview的行命令事件。并且尝试使用ItemTemplate将控件放入gridview中。

点击这里查看MSDN文档。GridView中的行命令

protected void Grid_RowCommand( object sender, GridViewCommandEventArgs e )
{
    int index = Convert.ToInt32( e.CommandArgument );
    your logic .......
}

您没有为命令参数添加值。对于.aspx页面中的Button事件

<asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" CommandArgument = 1 Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' /> 

后面的代码,即RowCommand事件

if(e.CommandName == "Test")
{
int index = Convert.ToInt32(e.CommandArgument);
}

这只适用于值1。为了使其通用,您可以使用绑定技术之一将命令参数绑定到您想要的值,例如:CommandArgument ='<%# Eval("ID") %>'(假设ID存在于GridView中)

查看这段代码

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);
  }
 }
}    
下面是gridview的html代码
<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>

查看链接Gridview命令