C# 获取GridView行

C# 获取GridView行,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,我在加载页面上绑定到一个SqlDataReader。它有一个带有按钮的列,我尝试在单击按钮时获取该行,代码如下: int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[index]; 编辑:从评论部分粘贴.aspx页面 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColo

我有一个GridView,我在加载页面上绑定到一个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);”

有什么想法吗?

请查看此代码

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 aspx代码中,您必须为Button控件使用命令名和命令参数,并且它应该绑定到db列之一。并使用gridview的行命令事件。并尝试使用ItemTemplate将控件放入gridview中

单击此处查看MSDN文档


您需要检查GridView行中已单击的命令。您的标记应该相应地映射。见下文egs。 您获得的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" />

您尚未为命令参数添加值。用于.aspx页面中的按钮事件

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

这仅适用于值1。要使其通用,您可以使用一种绑定技术将命令参数绑定到您想要的值,例如:
CommandArgument=''
(假设GridView中存在ID)

是否在int index=.上设置了断点。。检查了e.CommandArgument是什么?它很可能是一个无法转换为整数的值。您是否费心检查了e.CommandArgument的值?文档中说,当“值不包含后跟数字序列(0到9)的可选符号”时,将抛出
FormatException
。”是否在OnRowCommand上执行此操作?我检查了commandArgument,结果是“”。但为什么会这样呢@Emaad Ali是的,我正在GridView1_RowCommands上执行此操作。您可以在.aspx页面上设置命令参数吗?但错误在int index=Convert.ToInt32(e.CommandArgument)上@NevayShirazi非常有趣,所以当你点击按钮时,什么命令名出现在e.commandName中??如果(e.commandName==“Test”)int index=Convert.ToInt32(e.CommandArgument);GridViewRow行=((GridView)e.CommandSource).Rows[index];。它具有正确的命令,因为它位于If内部statement@Emaad阿里,你本可以更正你的答案,而不是在我的帖子上乱发-1。我试图表现得有建设性。你的答案需要更好的解释,而不是从其他地方复制和粘贴。抱歉,报告。按钮字段将自动使用该行的索引填充CommandArgument;使用常规按钮OP需要显式设置CommandArgument。只是澄清差异;你的方法是正确的。谢谢Tim,我正在用你的评论编辑答案。第一种方法只适用于第二行(CommandArgument=“1”)。第二种方法(绑定“ID”)可能不起作用,因为您(危险地)假设ID字段将匹配行索引。第一种方法只是一个示例,说明您需要在.aspx页面中指定命令参数。第二种方法是让他们知道他们也可以绑定事件。我知道它只与gridview中的一个列ID绑定。OP(或者稍后看到这个问题的人)可能没有意识到这是一个演示如何实现的示例。我建议编辑你的答案,让它更清楚-仅仅因为你(或我)知道你的意思并不意味着一个经验不足或全新的开发人员会。是的,我理解。我刚刚编辑了答案。让我知道这是否消除了混淆。:)
<asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" CommandArgument = 1 Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' /> 
if(e.CommandName == "Test")
{
int index = Convert.ToInt32(e.CommandArgument);
}