本文介绍了根据第一列中的值自动填充gridview的其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,如何根据第一栏中的值自动填充gridview的其他列



i尝试此代码但是它会出错

hello everybody how can Auto populating other columns of the gridview based on the value in the first column

i try this code but it gets error

private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
       {
           int count = dataGridView1.Columns.Count;
           if (e.RowIndex >= 0)
           {
               if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()))
               {
                   id1 = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                   populateColumns();

                   dataGridView1.Rows[e.RowIndex].Cells[4].Value = barcode;
                   dataGridView1.Rows[e.RowIndex].Cells[5].Value = price;
                   dataGridView1.Rows[e.RowIndex].Cells[6].Value = unit;
               }
           }

       }



       private void populateColumns()
       {OleDbConnection conConnect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Application.StartupPath + "\\Store.mdb");
           {
               string str = "SELECT Product.ID, Units.Unit, Product.barcode, Product.price FROM Units INNER JOIN Product ON Units.Un_id = Product.Un_id  WHERE proname " +
                 id1 + "';";
               OleDbCommand cmd =
                 new OleDbCommand(str, conConnect);
               OleDbDataAdapter da = new OleDbDataAdapter();
               da.SelectCommand = cmd;
               DataSet ds = new DataSet("dsEmployee");
               da.Fill(ds);

               conConnect.Open();

               foreach (DataRow row in ds.Tables[0].Rows)
               {
                   try
                   {
                       barcode = row["barcode"].ToString();
                       price = row["price"].ToString();
                       unit = row["Unit"].ToString();
                   }
                   catch (FormatException fEx)
                   {
                       MessageBox.Show(fEx.Message);
                   }
               }
           }
       }





错误是对象引用未设置为object.null referance unhandle如何完成



the error is Object reference not set to an instance of an object.null referance unhandle how it can be done

推荐答案


这篇关于根据第一列中的值自动填充gridview的其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:53