我用的VS2010,数据库是access。
我是要做一个计算器类型的简单软件。一个计算成本的界面,一个计算售价的界面,二个共用一个查询界面。
查询成本的界面没问题,导入dataGridView的时候正常,代码为
private void btntianjia_Click(object sender, EventArgs e)
        {
            if (txtzongjia.Text=="")
            {
                MessageBox.Show("请先进行价格计算在添加!", "提示");
                return;
            }
            Count c = new Count();
            c.id = txtshangpinbianhao.Text;
            c.name = txt.Text;
            c.ipice = double.Parse(txtchaxunjiage.Text);
            c.opice = double.Parse(textBox1.Text);
            c.mianji = double.Parse(txtmianji.Text);
            c.cmoney = double.Parse(txtzongjia.Text);
            c.lmoney = double.Parse(textBox2.Text);
            list.Add(c);
            dataGridView1.DataSource = new List<Count>(list);        }但是计算售价界面的代码写出来就不行了,导入的时候不按我写的导。代码为
  private void btntianjia_Click(object sender, EventArgs e)
        {
            if (txtzongjia.Text == "")
            {
                MessageBox.Show("请先计算价格再进行添加");
                return;
            }
            Count jsjg = new Count();            jsjg.id = txtshangpinbianhao.Text;
            jsjg.name = txt.Text;
            jsjg.mianji = double.Parse(txtmianji.Text);
            jsjg.opice = double.Parse(txtchaxunjiage.Text);
            
            //jsjg.zongmoney = double.Parse(txtzongjia.Text);
            list.Add(jsjg);
            dataGridView3.DataSource = new List<Count>(list);
        }

解决方案 »

  1.   

    那个list是在哪里定义的?有什么用?两个DataSource都从这个list构造。但这个list一直在Add,也没清过。
      

  2.   

     List<Count> list = new List<Count>();
    2个窗体都是用list来弄的。
      

  3.   

    现在有3个窗体,这3个窗体现在命名为A1 A2和B。A1和A2都是从B窗体中查询数据的。
    下面为B窗体的代码
    namespace WindowsFormsApplication1
    {
        public partial class chaxun : Form
        {
            public chaxun()
            {
                InitializeComponent();
            }
            public db d;
            private void button1_Click(object sender, EventArgs e)
            {
                if (dataGridView1.SelectedRows.Count==0)
                {
                    MessageBox.Show("请选择要查询的商品!","提示");
                    return;
                }
                d.wid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                d.wnum = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                d.wname = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                d.ipice = double.Parse(dataGridView1.SelectedRows[0].Cells[3].Value.ToString());
                d.opice = double.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
                
                this.Close();
                        }        private void txtchaxunbianhao_TextChanged(object sender, EventArgs e)
            {
                string id = txtchaxunbianhao.Text;
                string sql = string.Format("select * from db where wid like '%{0}%'", id);
                DataSet ds = OleDbHelper.GetDataSet(sql);
                dataGridView1.DataSource = ds.Tables[0];
            }        private void chaxun_Load(object sender, EventArgs e)
            {
                dataGridView1.AutoGenerateColumns = false;
            }
        }
    }
      

  4.   

    还是不大清楚lz是怎么个出错法,具体描述一下出错的地方...
    计算成本的界面和计算售价的界面里面的dataGridView结构是否是一样的?是否都可以直接将数据源绑定到List<Count>?
      

  5.   

    麻烦您去http://topic.csdn.net/u/20120111/11/21102fa5-b6de-4a17-a38d-a8f0c24ed1ee.html这看下吧。我从新提问的。。上面详细的说了下。下面也跟图了。谢谢啊