从gridview中选择列并传递给变量

时间:2013-01-22 13:57:38

标签: asp.net gridview datagridviewcolumn

下面是我尝试从10中选择列数据的代码,但是代码不能正常工作

  string strSuppId = GridView1.SelectedRow.Cells[10].Text;

错误MSG:对象引用未设置为对象的实例。

我的标记

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="pProductId" DataSourceID="AccessDataSource1">
    <Columns>
        <asp:BoundField DataField="pProductId" HeaderText="pProductId" 
            InsertVisible="False" ReadOnly="True" SortExpression="pProductId" />
        <asp:BoundField DataField="pProductName" HeaderText="pProductName" 
            SortExpression="pProductName" />
        <asp:BoundField DataField="pProductImage" HeaderText="pProductImage" 
            SortExpression="pProductImage" />
        <asp:BoundField DataField="pProductType" HeaderText="pProductType" 
            SortExpression="pProductType" />
        <asp:BoundField DataField="pProductWeight" HeaderText="pProductWeight" 
            SortExpression="pProductWeight" />
        <asp:BoundField DataField="pProductDesign" HeaderText="pProductDesign" 
            SortExpression="pProductDesign" />
        <asp:BoundField DataField="PProductColor" HeaderText="PProductColor" 
            SortExpression="PProductColor" />
        <asp:BoundField DataField="pProductTransparency" 
            HeaderText="pProductTransparency" SortExpression="pProductTransparency" />
        <asp:BoundField DataField="pProductQuantity" HeaderText="pProductQuantity" 
            SortExpression="pProductQuantity" />
        <asp:BoundField DataField="pProductUnitPrice" HeaderText="pProductUnitPrice" 
            SortExpression="pProductUnitPrice" />
        <asp:BoundField DataField="pSupplierId" HeaderText="pSupplierId" 
            SortExpression="pSupplierId" />
    </Columns>

我的代码文件

protected void btnAddPdt_Click(object sender, EventArgs e)
    {
        if (txtPdtName.Text == "" || ddlPdtType.SelectedItem.Text == "" || txtPrice.Text == "")
        {
            MessageBox.Show("Fields cannot be empty", "Error Message");
        }
        else
        {
            OleDbConnection mDB = new OleDbConnection();
            mDB.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source="
            + Server.MapPath("~/App_Data/Database.accdb");
            mDB.Open();
            Type csType = this.GetType();
            OleDbCommand cmd;


            string strSuppId = GridView1.SelectedRow.Cells[10].Text;

            string sqlInsert = "INSERT INTO "
                + " Products (pProductName, pProductType, pProductWeight, pProductDesign, pProductColor, pProductTransparency, pProductQuantity, pProductUnitPrice, pSupplierId)"
                + " VALUES (@pdtName, @pdtType, @pdtWt, @pdtDesign, @pdtColor, @pdtTrans, @pdtQty, @pdtPrice, @pSuppId)";

            cmd = new OleDbCommand(sqlInsert, mDB);
            cmd.Parameters.AddWithValue("@pdtName", txtPdtName.Text);
            cmd.Parameters.AddWithValue("@pdtType", ddlPdtType.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@pdtWt", txtPdtWt.Text);
            cmd.Parameters.AddWithValue("@pdtDesign", txtDesign.Text);
            cmd.Parameters.AddWithValue("@pdtColor", txtColor.Text);
            cmd.Parameters.AddWithValue("@pdtTrans", ddlTrans.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@pdtQty", txtQuantity.Text);
            cmd.Parameters.AddWithValue("@pdtPrice", txtPrice.Text);
            cmd.Parameters.AddWithValue("@pSuppId", strSuppId);

            cmd.ExecuteNonQuery();
            mDB.Close();
    }

1 个答案:

答案 0 :(得分:0)

尝试以下编码......

将变量声明为全局

public string _Data = "";

编写Gridview SelectedIndexed事件,如下所示

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
    string data = "";
    GridViewRow row = GridView1.SelectedRow;
    _Data = row.Cells[10].Text;
}

然后更改您的编码,如下所示

string strSuppId = GridView1.SelectedRow.Cells[10].Text;

to

string strSuppId = _Data

在Button_Click之前从GridView中选择行...然后只返回数据....