日期:2014-05-18  浏览次数:21044 次

DataGridView 组合字段自动更新到数据库
小弟现在做一个数据检查的小工具遇到这样一个问题。下面作详细说明:
  数据库设计是这样的,二个字段 nFieldType,sFieldType 分别代表字段类型ID,以及相应的说明,
  比如一条记录存放,则为 nFieldType=1 sFieldType=integer 
  在DataGridView 里显示成DataGridViewComboBoxColumn 下拉框,里面有四个项,显示的就是相应的值.
   
  在显示时要用 select nFieldType from tUser 来与下拉框设置的值对应

  显示没问题,但在修改后,nFiedType 不会有错,但sFieldType不会随之改变,请问如何解决这样的问题呀?
   
  下面为源码:
 
C# code

    public partial class DGVUpdateDB : Form
    {
        private DataTable dt = new DataTable();
        private SqlDataAdapter sda = new SqlDataAdapter();

        public DGVUpdateDB()
        {
            InitializeComponent();
            this.dataGridView1.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e) { };
            dataList_FieldType dFieldType = new dataList_FieldType();
            Name.DataPropertyName = "name";
            age.DataPropertyName = "age";
            serid.DataPropertyName = "serid";
            FieldType.DataSource = dFieldType;
            FieldType.ValueMember = "nFieldType";
            FieldType.DisplayMember = "sFieldType";
            FieldType.DataPropertyName = "nFieldType";
        }

        private void DGVUpdateDB_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=127.0.0.1;database=Test_DB;uid=sa;pwd=123456");
            SqlCommand scd = new SqlCommand("select name,age,serid,nFieldType from tUser", conn);
            sda.SelectCommand = scd;
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
        }

       //更新数据库
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                sda.Update(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            MessageBox.Show("更新成功!");
        }

    }

   //下拉框内容
    class dataList_FieldType : DataTable
    {
        public DataColumn nFieldType = null;
        public DataColumn sFieldType = null;

        public dataList_FieldType()
        { 
           //为DataTable定义两列
            nFieldType = new DataColumn();
            sFieldType = new DataColumn();

            nFieldType.ColumnName = "nFieldType";
            sFieldType.ColumnName = "sFieldType";

            nFieldType.DataType = typeof(int);
            sFieldType.DataType = typeof(string);

            //将列加入到DataTable
            this.Columns.Add(nFieldType);
            this.Columns.Add(sFieldType);

            DataRow dr = this.NewRow();
            dr["nFieldType"] = 1;
            dr["sFieldType"] = "1.Integer";
            this.Rows.Add(dr);
            
            dr = this.NewRow();
            dr["nFieldType"] = 2;
            dr["sFieldType"] = "2.numeric";
            this.Rows.Add(dr);

            dr = this.NewRow();
            dr["nFieldType"] = 3;
            dr["sFieldType"] = "3.varchar";
            this.Rows.Add(dr);

            dr = this.NewRow();
            dr["nFieldType"] = 4;
            dr["sFieldType"] = "4.datetime";
            this.Rows.Add(dr);
        }

   


------解决方案--------------------
dt更新前你看下dt是否是你要的