获取GridView行

时间:2011-08-19 18:38:48

标签: c# asp.net gridview

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

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

编辑:从评论部分粘贴.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:输入字符串的格式不正确。在线'int index = Convert.ToInt32(e.CommandArgument);'。

有什么想法吗?

4 个答案:

答案 0 :(得分:6)

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

在CodeBehind中:

    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" />

答案 1 :(得分:1)

你可以发布整个标记代码,这将有助于解决。根据你的问题 在gridview aspx代码中,您必须使用Command Name,并为Button Control命令Argument,它应该绑定到db的列之一。并使用gridview的Row Command事件。并且还尝试使用ItemTemplate将Control放在gridview中。

单击此处获取MSDN文档。 Row Command in GridView

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

答案 2 :(得分:0)

您没有为命令Argument添加值。对于.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.为了使其成为通用,您可以使用其中一种绑定技术将命令Argument绑定到所需的值,例如:CommandArgument ='<%# Eval("ID") %>'(假设GridView中存在ID)< / p>

答案 3 :(得分:-1)

查看此代码

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 commands

希望这个答案可以帮到你。