遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

一些动态绑定数据代码

复制代码
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
复制代码

 

posted on   遗忘海岸  阅读(32)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
历史上的今天:
2010-08-13 多级分类,按用户配置可选-使用三组ListBox(参考taobao发布)
2008-08-13 Asp.net2.0中的提供程序---dashcommerce中的税率计算提供程序
2007-08-13 IssueVision学习(前话)
点击右上角即可分享
微信分享提示