当前位置: 代码迷 >> C# >> dataGridView更新解决方法
  详细解决方案

dataGridView更新解决方法

热度:158   发布时间:2016-04-28 08:43:08.0
dataGridView更新
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1   
{
    public partial class SingleStoreReplenishment : Form
    {
        private DataTable dt = new DataTable();
        private DataSet ds = new DataSet();
        private SqlDataAdapter da = new SqlDataAdapter();
        public int sum = 0;
        public SingleStoreReplenishment()
        {
            InitializeComponent();
        }

        private void SingleStoreReplenishment_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“polihua001DataSet1.PurchaseTable”中。您可以根据需要移动或移除它。
            //this.purchaseTableTableAdapter1.Fill(this.polihua001DataSet1.PurchaseTable);
            // TODO: 这行代码将数据加载到表“polihua001DataSet.UsreInfo”中。您可以根据需要移动或移除它。
            this.usreInfoTableAdapter.Fill(this.polihua001DataSet.UsreInfo);
            // TODO: 这行代码将数据加载到表“polihua001DataSet.PurchaseTable”中。您可以根据需要移动或移除它。
            this.purchaseTableTableAdapter.Fill(this.polihua001DataSet.PurchaseTable);
            // TODO: 这行代码将数据加载到表“polihuaTest.ProductClassification”中。您可以根据需要移动或移除它。
            this.productClassificationTableAdapter.Fill(this.polihuaTest.ProductClassification);
            this.slblLoginName.Text = UserHepler.logname;
            //绑定门店名称在comboBox1上,去掉重复的门店名称
            try
            {
                //查询门店名称的SQL语句
                string sql = "select UserAddres from UsreInfo group by UserAddres";
                //定义Command对象
                SqlCommand comma = new SqlCommand(sql, DBHelper.conn);
                //打开数据库
                DBHelper.conn.Open();
                //执行数据库查询
                SqlDataReader sdr = comma.ExecuteReader();
                string mdmc = "";
                //循环读取所有门店的名称,并添加到comboBox1中
                while (sdr.Read())
                {
                    mdmc = (string)sdr[0];
                    comboBox1.Items.Add(mdmc);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库错误!");
            }
            finally 
            {
                DBHelper.conn.Close();
            }

        }
        /// <summary>
        /// 根据选择的门店名称查询出门店的订单信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            string itemtxt = comboBox1.Text;
            //根据门店名称查询门店采购单信息
            string sql = string.Format("select CommodityName,OrderQuantity ,ReplenishmentQuantity ,OrderSum  from PurchaseTable where StoreName = '{0}'", itemtxt);
             try 
                {
                 //打开连接
                    SqlCommand comm = new SqlCommand(sql, DBHelper.conn);
                    DBHelper.conn.Open();

                }catch(Exception ex)
                {
                    Console.Write(ex.Message);
                }
                finally
                {
                    DBHelper.conn.Close();
                }
                SqlDataAdapter da = new SqlDataAdapter(sql, DBHelper.conn);
                
                da.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.AllowUserToAddRows = false;
          }
        /// <summary>
        /// 向数据库中插入补够数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要修改采购数量吗?","保存操作",MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK) 
            {
                SqlCommandBuilder builder = new SqlCommandBuilder(da);
                da.Update(ds, "0");
            }
        }
        /// <summary>
        /// 对补购数量进行加减计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Rows[e.RowIndex].Cells["Column4"].Value == null)
            {
                this.dataGridView1.Rows[e.RowIndex].Cells["Column4"].Value = 0;
            }
            int i = (int)dataGridView1.Rows[e.RowIndex].Cells["Column4"].Value;
            if (e.ColumnIndex == 0)
            {
                //加
                this.dataGridView1.Rows[e.RowIndex].Cells["Column4"].Value = ++i;
                //Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["Column6"].Value) +=1;
                int value = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells["Column6"].Value.ToString());
                this.dataGridView1.Rows[e.RowIndex].Cells["Column6"].Value = value + 1;

            }
            if (e.ColumnIndex == 2)
            {
                if (i > 0)
                {
                    //减
                    this.dataGridView1.Rows[e.RowIndex].Cells["Column4"].Value = --i;
                    int value = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells["Column6"].Value.ToString());
                    this.dataGridView1.Rows[e.RowIndex].Cells["Column6"].Value = value - 1;
                    
                }
            }
        }
    }
}
报错内容为:Update 无法找到 TableMapping['0'] 或 DataTable“0”
报错图片:

怎么修改??
------解决思路----------------------
  finally
                {
                    DBHelper.conn.Close();
                }
                SqlDataAdapter da = new SqlDataAdapter(sql, DBHelper.conn);   你这里都已经关了连接了 再取当然没值
------解决思路----------------------

private void button6_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定要修改采购数量吗?","保存操作",MessageBoxButtons.OKCancel);
if (result == DialogResult.OK) 
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DBHelper.conn.Open();
da.Update(ds.Tables[0]);
DBHelper.conn.Close();
}
}
  相关解决方案