【问题标题】:Get GridView Row获取 GridView 行
【发布时间】:2011-08-19 18:38:48
【问题描述】:

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

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

编辑:从 cmets 部分粘贴 .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);'。

有什么想法吗?

【问题讨论】:

  • 您是否在 int index=.. 上设置了断点并检查了 e.CommandArgument 是什么?它很可能是一个无法转换为整数的值。
  • 您是否费心检查 e.CommandArgument 的值?文档说FormatException 将在“值不包含可选符号后跟一系列数字(0 到 9)”时抛出。 msdn.microsoft.com/en-us/library/sf1aw27b.aspx
  • 你是在 OnRowCommand 上做这个吗?
  • 我检查了 commandArgument 并且是 ""。但是为什么会发生这种情况? @Emaad Ali 是的,我在 GridView1_RowCommand 上这样做
  • 你能把你设置了命令参数的.aspx页面贴出来吗?

标签: c# asp.net gridview


【解决方案1】:

您需要检查 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" />

【讨论】:

  • ButtonField 将自动用行的索引填充 CommandArgument;使用常规按钮 OP 需要显式设置 CommandArgument。只是澄清差异;你的方法是正确的。
  • 谢谢蒂姆,我正在用你的评论编辑答案。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    您没有为命令参数添加值。对于 .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 ='&lt;%# Eval("ID") %&gt;'(假设 ID 存在于 GridView 中)

    【讨论】:

    • 第一种方式仅适用于第二行 (CommandArgument = "1")。您的第二种方法(“ID”上的绑定)可能不起作用,因为您做出(危险)假设 ID 字段将匹配行索引。
    • 第一个只是一个示例,表明您需要在 .aspx 页面中指定命令参数。第二种方法是让他们知道他们也可以绑定事件。我知道它只与来自 gridview 的列 ID 绑定。
    • OP(或有相同问题的人稍后会查看此问题)可能没有意识到这是一个展示如何完成的示例。我建议编辑您的答案以使其更清楚 - 仅仅因为您(或我)知道您的意思并不意味着经验不足或全新的开发人员会。
    • 是的,我明白了。我刚刚编辑了答案。让我知道这是否消除了混乱。 :)
    【解决方案4】:

    查看此代码

    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

    希望这个答案对你有所帮助。

    【讨论】:

    • 但错误出现在 int index = Convert.ToInt32(e.CommandArgument);
    • @nevayeshirazi 非常有趣的朋友
    • 好的,所以当您点击按钮时,e.commandName 中的命令名称是什么??
    • if (e.CommandName == "Test") int index = Convert.ToInt32(e.CommandArgument); GridViewRow 行 = ((GridView)e.CommandSource).Rows[index];.它具有正确的命令,因为它位于 If 语句中
    • @Emaad Ali,您本可以更正您的答案,而不是向我的帖子发送垃圾邮件 -1。我试图成为建设性的。您的答案需要更好的解释,而不仅仅是从其他地方复制和粘贴。抱歉报告了。
    猜你喜欢
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多