c#从DataGridView中的单元格检索数据时获取NullReferenceException

c#从DataGridView中的单元格检索数据时获取NullReferenceException,c#,datagridview,nullreferenceexception,C#,Datagridview,Nullreferenceexception,这是我的代码: private void CostList_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'lSEStockDataSet.CostPrice' table. You can move, or remove it, as needed. this.costPriceTableAdapter.Fill(this.lSEStockDataSet.Cos

这是我的代码:

private void CostList_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'lSEStockDataSet.CostPrice' table. You can move, or remove it, as needed.
    this.costPriceTableAdapter.Fill(this.lSEStockDataSet.CostPrice);

    con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = "Data Source=tcp:SHEN-PC,49172\\SQLEXPRESS;Initial Catalog=LSEStock;Integrated Security=True";
    con.Open();

    DataGridView datagridview1 = new DataGridView();
    String retrieveData = "SELECT CostID, SupplierName, CostPrice FROM CostPrice WHERE PartsID ='" + textBox1.Text + "'";
    SqlCommand cmd = new SqlCommand(retrieveData, con);
    int count = cmd.ExecuteNonQuery();
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    dataGridView1.DataSource = dt;
    con.Close();

}

private void button1_Click(object sender, EventArgs e)
{

    if (dataGridView1.Rows.Count > 0)
    {   
        int nRowIndex = dataGridView1.Rows.Count-1;


        if (dataGridView1.Rows[nRowIndex].Cells[2].Value != null)
        {
            textBox2.Text = Convert.ToString(dataGridView1.Rows[nRowIndex].Cells[2].Value);
        }
        else
        {
            MessageBox.Show("NULL");
        }
    }
}

当我点击按钮时,它显示NULL,这里有什么问题?我有3列,我想获取最后一行第3列的数据,但它显示NULL,但指定单元格中有数据。有人知道如何解决这个问题吗?

请尝试从行数中减去2,而不是从行数中减去1。减去1即得到“add”行的从零开始的索引,它在最后一列中确实有一个空值

    int nRowIndex = dataGridView1.Rows.Count-2;
通过从计数中减去2,您将得到最后一行的从零开始的索引,其中包含实际数据。我想这就是你要找的

另外,您可能希望参数化SQL查询,如下所示:

String retrieveData = "SELECT CostID, SupplierName, CostPrice FROM CostPrice WHERE PartsID = @inPartsID";
SqlCommand cmd = new SqlCommand(retrieveData, con);
cmd.Parameters.Add(new SqlParameter("@inPartsID", textBox1.Text));

这将使您的查询更加可靠(如果textBox1中只有一个引号字符,会发生什么情况)并使您的数据更加安全(作恶者可以使用SQL注入对您的数据库造成损害,或者从中获取他们不应该得到的数据)

不要从行计数中减去一,而是尝试减去两。减去1即得到“add”行的从零开始的索引,它在最后一列中确实有一个空值

    int nRowIndex = dataGridView1.Rows.Count-2;
通过从计数中减去2,您将得到最后一行的从零开始的索引,其中包含实际数据。我想这就是你要找的

另外,您可能希望参数化SQL查询,如下所示:

String retrieveData = "SELECT CostID, SupplierName, CostPrice FROM CostPrice WHERE PartsID = @inPartsID";
SqlCommand cmd = new SqlCommand(retrieveData, con);
cmd.Parameters.Add(new SqlParameter("@inPartsID", textBox1.Text));

这将使您的查询更加可靠(如果textBox1中只有一个引号字符,会发生什么情况)并使您的数据更加安全(作恶者可以使用SQL注入对您的数据库造成损害,或者从中获取他们不应该得到的数据)

您的
DataGridView
是否将
allowUserToAddress
属性设置为
true
?您是否可以共享页面加载代码?问题已通过以下答案解决,谢谢=)您的
DataGridView
是否将
allowUserToAddress
属性设置为
true
?您是否可以共享页面加载请输入代码?问题已通过下面的答案解决谢谢=)谢谢你的帮助,你让我的日子=D btw,文本框1。文本不可编辑,所以我猜不会有任何问题。谢谢你的帮助,你让我的日子=D btw,文本框1。文本不可编辑,所以我猜不会有任何问题。