【问题标题】:Trouble grabbing Row Number from GridView无法从 GridView 中获取行号
【发布时间】:2013-11-08 10:05:32
【问题描述】:

我有以下 GridView:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="SysInvoiceID" HeaderText="SysInvoiceID" ReadOnly="True" SortExpression="SysInvoiceID" />
                <asp:BoundField DataField="BillMonth" HeaderText="BillMonth" SortExpression="BillMonth" />
                <asp:BoundField DataField="InvoiceDate" HeaderText="InvoiceDate" ReadOnly="True" SortExpression="InvoiceDate" />
                <asp:BoundField DataField="InvoiceNumber" HeaderText="InvoiceNumber" SortExpression="InvoiceNumber" />
                <asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
                <asp:BoundField DataField="VAT" HeaderText="VAT" SortExpression="VAT" />
                <asp:BoundField DataField="Gross" HeaderText="Gross" SortExpression="Gross" />
                <asp:ButtonField CommandName="ViewInvoice"  HeaderText=" " ShowHeader="True" Text="View" />
            </Columns>
        </asp:GridView>

这是页面背后的代码:

public partial class PagingTest01 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

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 == "ViewInvoice")
    {
        // 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 = GridView1.Rows[index];
        // Now you have access to the gridviewrow.

        ViewButton_Click(row);
    }
}




protected void ViewButton_Click(GridViewRow row)
{ 
    byte[] FileImage = GetImageData(0,row);

      if (FileImage != null)
      {
          base.Response.Clear();
          base.Response.Buffer = true;
          base.Response.ContentType = "Application/x-pdf";
          base.Response.ContentEncoding = Encoding.Default;
          string attachment = string.Format("attachment;filename=\"Invoice_{0}.pdf\"", "Customer1");
          base.Response.AddHeader("content-disposition", attachment);
          base.Response.BinaryWrite(FileImage);
          base.Response.Flush();
          base.Response.Close();
          base.Response.End();
      }
}





public byte[] GetImageData(int sysInvoiceID,  GridViewRow row)
    {

        string strUserID = CommonCode.GetCurrentUserID().ToString();
        string strCustomerID = CommonCode.GetCurrentCustomerID().ToString();
        byte[] numArray;

        string strConnectionString = "Data Source=TESTSERV;Initial Catalog=DB_Invoices;Persist Security Info=True";
        SqlConnection connection = new SqlConnection(strConnectionString);
        SqlCommand command = new SqlCommand("select FileImage from DB_Invoices.dbo.Bills WHERE (FileType = 'PDF' AND SysInvoiceID = @ID)", connection);
        command.Parameters.AddWithValue("@ID", GridView1.Rows[row].Cells[0].Text);

        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();

        try
        {
            connection.Open();



            da.Fill(ds);
            DataRow item = ds.Tables[0].Rows[0];
            byte[] item1 = (byte[])item["FileImage"];
            ds.Tables.Clear();
            numArray = item1;


        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }
        return numArray;
    }



}

所以基本上我有一个包含很多行的 GridView,每个行旁边都有一个“视图”按钮域。单击“查看”时,我尝试使用 GridView1_RowCommand,它应该希望在将单击的行传递到 ViewButton_Click 之前抓住单击的行。然后这将调用 GetImageData 并将行号传递到这一行:

command.Parameters.AddWithValue("@ID", GridView1.Rows[row].Cells[0].Text);

单元格 0 是 SysInvoiceID 列,因此如果传递了正确的行,@ID 将被分配一个 SysInvoiceID。

'Row' 然而似乎不是一个有效的论点,虽然我想不出为什么不......除非我必须明确地将它转换为一个 int?任何帮助将非常感激!谢谢。

【问题讨论】:

  • 旁注:如果IDint,你应该使用int.Parse(celltext),否则数据库会得到错误的类型,因为AddWithValue 需要从值推断类型。跨度>
  • 刚刚注意到一些事情......如果我明确输入行号(例如 1),至少在代码可以工作之前,但目前什么都没有发生。从外观上看,我认为 ViewButton_Click 没有被正确调用?

标签: c# asp.net gridview gridviewrow


【解决方案1】:

我刚刚将此评论为 side-note 但也许这是您的问题,因为您提到 “这似乎不是一个有效的论点,尽管我不知道为什么不是...除非我必须将其显式转换为 int".

如果IDint,你应该使用int.Parse(celltext),否则数据库会得到错误的类型,因为AddWithValue 需要从值推断类型。

所以使用:

command.Parameters.AddWithValue("@ID", int.Parse(GridView1.Rows[row].Cells[0].Text));

除此之外,您还没有添加事件处理程序GridView1_RowCommand

<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
   ....

而且您也没有将CommandArgument 设置为行的索引。如果您需要行索引,无论如何我都会使用不同的方法。使用模板字段和控件,例如Button。然后使用它的NamingContainer 属性来获取对'GridViewRow'的引用,这里是一个例子:Get Row Index on Asp.net Rowcommand event

【讨论】:

  • 感谢您的信息,我不知道!虽然是的,但这并没有解决我遇到的特定问题。
  • @JimmyK:编辑了我的答案,你还没有添加事件处理程序GridView1_RowCommand 和其他一些东西。
  • 在大家的帮助下成功完成了这项工作!既然你给了我最大的支持,我会把它标记为答案,非常感谢!
【解决方案2】:

使用这个:

int index = ((GridViewRow)((WebControl)sender)).RowIndex;

代替

int index = Convert.ToInt32(e.CommandArgument);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多