dataGridView更新数据库

Jasonchen0821 2015-10-27 03:27:27
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)
{
string sql = string.Format("select * from PurchaseTable");

DialogResult result = MessageBox.Show("确定要修改采购数量吗?","保存操作",MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
SqlCommand command = new SqlCommand(sql, DBHelper.conn);
SqlDataAdapter adad = new SqlDataAdapter(command);
command.CommandType = CommandType.Text;
adad.SelectCommand = command;
SqlCommandBuilder builder = new SqlCommandBuilder(adad);
adad.Fill(ds.Tables[0]);
this.dataGridView1.DataSource = ds.Tables[0];
adad.UpdateCommand = builder.GetUpdateCommand();
DBHelper.conn.Open();
da.Update(ds.Tables[0]);
DBHelper.conn.Close();
}
}
/// <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;

}
}
}
}
}
抛出“对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”的异常,
求解!!!!
...全文
70 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧