一些动态绑定数据代码

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;
using F.Studio.Data.Util;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        private List<DataRowView> DeleteRows = new List<System.Data.DataRowView>();
       
        private string ConnStr = @"data source=192.168.1.91;initial catalog=JL_MFG;persist security info=True;user id=xxxx;password=xxxx;multipleactiveresultsets=True;persist security info=True;";
        public Form1()
        {
            InitializeComponent();

 
            #region 绑定完后加载
            var dt = GetDataTable(1);
            bindingSource1.DataSource = dt;//加载架构信息先进行绑定
            var lbl = new LabelControl() { Text = "关键字2342多搞点中文" };
            var p = GetFontSize(lbl.Font, lbl.Text);
            lbl.Location = new Point(20, 20);
            var txt1 = new TextEdit();
            txt1.Name = "txtKey";
            txt1.DataBindings.Add("EditValue", bindingSource1, "AddEmpNo");
            var x = (int)(p.Width);
            txt1.Location = new Point(x + lbl.Location.X, 20);
            panelControl1.Controls.Add(lbl);
            panelControl1.Controls.Add(txt1);

            #endregion

        }
        private SizeF GetFontSize(Font font, string txt)
        {
          Graphics g = this.CreateGraphics();
          SizeF sizeF = g.MeasureString(txt, font);
          g.Dispose();
          return sizeF;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            bindingSource1.DataSource = GetDataTable();
            gridView1.DataSourceChanged += new EventHandler(gridView1_DataSourceChanged);
        }

        void gridView1_DataSourceChanged(object sender, EventArgs e)
        {
            Console.WriteLine("DataSourceChanged");
            var columns = gridView1.Columns.OfType<GridColumn>().ToList();
            foreach (var c in columns)
            {
               // c.OptionsColumn.FixedWidth
                c.Width = 200;
               if(c.FieldName=="RecId"){
                   c.VisibleIndex = 10;
               }

                Console.WriteLine(c.FieldName +"," + c.Width +","+c.SortIndex);


            }

        }
        private DataTable GetDataTable(int mode=0)
        {
            var sql="select * from XL_AB_Salary order by recId";
            if (mode == 1)
            {


                SqlConnection conn = new SqlConnection(ConnStr);
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                var ds = new DataSet();
                da.FillSchema(ds, SchemaType.Mapped);
                return ds.Tables[0];
            }
            else
            {
                return SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql).Tables[0];
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            bindingSource1.EndEdit();
            var its = panelControl1.Controls.Find("txtKey", true);
            if (its != null && its.Count() > 0)
            {
                var txt = its[0] as TextEdit;
                Console.WriteLine(txt.EditValue);
            }
            var dt = bindingSource1.DataSource as DataTable;
        
           
            var cDt= dt.GetChanges();
            if (cDt == null) return;
            foreach (DataRow r in cDt.Rows)
            {
                var id = r["RecId", DataRowVersion.Original];
                Console.WriteLine(id);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
      
            var cur = bindingSource1.Current as DataRowView;
            if (cur == null) return;

            DeleteRows.Add(cur);
            cur.Delete();
            
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            var newRow= bindingSource1.AddNew();
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/wdfrog/p/15138471.html