• 一些动态绑定数据代码


    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
  • 相关阅读:
    2018年奇虎360春招笔试题--玫瑰花
    MaxPooling的作用
    网易笔试编程题:被3整除
    网易笔试编程题:牛牛找工作
    剑指offer 第十二天
    算法题:合并N个长度为L的有序数组为一个有序数组(JAVA实现)
    十分钟看懂神经网络反向传输算法
    十大经典排序算法最强总结(含JAVA代码实现)
    记服务器中招挖矿病毒排查过程(解决方案篇)
    spring 事件监听同时支持同步事件及异步事件
  • 原文地址:https://www.cnblogs.com/wdfrog/p/15138471.html
Copyright © 2020-2023  润新知