找回密码
 立即注册

QQ登录

只需一步,快速开始

zg0909

注册会员

9

主题

15

帖子

145

积分

注册会员

积分
145
zg0909
注册会员   /  发表于:2017-10-15 16:34  /   查看:2833  /  回复:1
本帖最后由 zg0909 于 2017-10-15 16:36 编辑

找到个例子,但是用的是虚表,看不到保存到数据库的效果。
改了一下,红色部分。还是不行。
CREATE TABLE [dbo].[test](
        [autoid] [int] IDENTITY(1,1) NOT NULL,
        [itm] [int] NULL,
        [name] [varchar](50) NULL,
        [dd] [datetime] NULL,
        [amtn] [numeric](28, 8) NULL,
        [tt] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

-------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace BoundFinishEdit
{
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
                private System.Windows.Forms.ToolBar toolBar1;
                private System.Windows.Forms.ToolBarButton toolBarButton1;
                private System.Windows.Forms.Button button1;
                private C1.Win.C1FlexGrid.C1FlexGrid _flex;
        private Button button2;
        private Button button3;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

                public Form1()
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

                        //
                        // TODO: Add any constructor code after InitializeComponent call
                        //
                }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this._flex = new C1.Win.C1FlexGrid.C1FlexGrid();
            this.toolBar1 = new System.Windows.Forms.ToolBar();
            this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this._flex)).BeginInit();
            this.SuspendLayout();
            //
            // _flex
            //
            this._flex.ColumnInfo = "10,1,0,0,0,75,Columns:";
            this._flex.Dock = System.Windows.Forms.DockStyle.Fill;
            this._flex.Location = new System.Drawing.Point(0, 41);
            this._flex.Name = "_flex";
            this._flex.Rows.DefaultSize = 20;
            this._flex.Size = new System.Drawing.Size(512, 232);
            this._flex.StyleInfo = resources.GetString("_flex.StyleInfo");
            this._flex.TabIndex = 0;
            this._flex.UseCompatibleTextRendering = true;
            //
            // toolBar1
            //
            this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
            this.toolBarButton1});
            this.toolBar1.DropDownArrows = true;
            this.toolBar1.Location = new System.Drawing.Point(0, 0);
            this.toolBar1.Name = "toolBar1";
            this.toolBar1.ShowToolTips = true;
            this.toolBar1.Size = new System.Drawing.Size(512, 41);
            this.toolBar1.TabIndex = 1;
            this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
            //
            // toolBarButton1
            //
            this.toolBarButton1.Name = "toolBarButton1";
            this.toolBarButton1.Text = "Save";
            this.toolBarButton1.ToolTipText = "Save";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(96, 4);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(211, 35);
            this.button1.TabIndex = 2;
            this.button1.Text = "Save (this one gets the focus!)";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(334, 4);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(64, 25);
            this.button2.TabIndex = 3;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(404, 4);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(64, 25);
            this.button3.TabIndex = 4;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(512, 273);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this._flex);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.toolBar1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "C1FlexGrid: LostFocus and DataRowState";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this._flex)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

                }
                #endregion

                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                        Application.Run(new Form1());
                }


        DataTable _dt;

        private void Form1_Load(object sender, System.EventArgs e)
        {
            _dt = new DataTable("test");


            //_dt.Columns.Add("Name", typeof(string));
            //_dt.Columns.Add("Age", typeof(int));


            //object[] data = new object[2];
            //string[] names = "Sue|Ann|Mary|Bob|Joe".Split('|');
            //Random rnd = new Random();
            //for (int i = 0; i < 30; i++)
            //{
            //    data[0] = names[rnd.Next(names.Length - 1)];
            //    data[1] = rnd.Next(6, 102);
            //    _dt.Rows.Add(data);
            //}
            //_dt.AcceptChanges(); // << added->unchanged

            SqlDataAdapter sda = new SqlDataAdapter("select * from test", "server=.;database=test;uid=sa;pwd='7788'");
            sda.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            ////        SqlCommandBuilder commandbuiler;
            ////        commandbuiler = new SqlCommandBuilder(sda);
            sda.Fill(_dt);


            _flex.DataSource = _dt;
        }
        private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
                {
                        // the toolbar doesn't get the focus when clicked, so
                        // the grid could still be in edit mode...
                        _flex.FinishEditing();

                        // the grid still has the focus and the cursor hasn't moved to a new row,
                        // so the data source still doesn't see the row as changed.
                        // so we use the currency manager to end the edits.
                        CurrencyManager cm = (CurrencyManager)BindingContext[_flex.DataSource, _flex.DataMember];
                        cm.EndCurrentEdit();

                        // proceed as usual
                        button1_Click(sender, EventArgs.Empty);
                }


                private void button1_Click(object sender, System.EventArgs e)
                {
                        Console.WriteLine("*** Checking for changes...");

                        // check which rows have been modified
                        int cnt = 0;
                        foreach (DataRow dr in _dt.Rows)
                                if (dr.RowState == DataRowState.Modified)
                                        cnt++;
                        Console.WriteLine("\t{0} changed row(s).", cnt);

                        // another way
                        DataTable dtChanges = _dt.GetChanges(DataRowState.Modified);
                        Console.WriteLine("\t{0} changed row(s).", (dtChanges != null)? dtChanges.Rows.Count: 0);

                        // commit changes
                        _dt.AcceptChanges();
                }

        private void button2_Click(object sender, EventArgs e)
        {
            _flex.Rows.Add();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            _flex.Rows.Remove(1);
        }
    }
}


1 个回复

倒序浏览
JeffryLI
葡萄城公司职员   /  发表于:2017-10-16 09:45:01
沙发
您好,没有实际的数据库交互的实例,这块把数据发送到后台就可以用您的数据处理层来处理,处理的方式很多,可以使存储过程,也可以是您直接写sql语句,这块的内容在网上也很多,您可以查找一下,希望能帮到您。
请点击评分,对我5分评价,谢谢!

葡萄城控件服务团队
官方网站: https://www.grapecity.com.cn/developer
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部