Re[4]: Вывести данные на DataGrid из кода
От: DemAS http://demas.me
Дата: 18.03.03 16:11
Оценка:
Здравствуйте, Alex Medvedev, Вы писали:

AM>Необходимо вызвать AcceptChanges.


Не помогает.

Вот полный код:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using musicBoxCode.Classes;


namespace musicBoxCode.Forms
{
    /// <summary>
    /// Summary description for Balls.
    /// </summary>
    public class Balls : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DataGrid dataGrid;
        private System.Windows.Forms.DataGridTableStyle dataGridTableStyle;
        private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn_id;
        private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn_name;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        private DataSet ds;

        public Balls()
        {
            //
            // 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()
        {
            this.dataGrid = new System.Windows.Forms.DataGrid();
            this.dataGridTableStyle = new System.Windows.Forms.DataGridTableStyle();
            this.dataGridTextBoxColumn_id = new System.Windows.Forms.DataGridTextBoxColumn();
            this.dataGridTextBoxColumn_name = new System.Windows.Forms.DataGridTextBoxColumn();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid
            // 
            this.dataGrid.DataMember = "";
            this.dataGrid.Dock = System.Windows.Forms.DockStyle.Top;
            this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid.Name = "dataGrid";
            this.dataGrid.Size = new System.Drawing.Size(242, 200);
            this.dataGrid.TabIndex = 0;
            this.dataGrid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                                                                                                 this.dataGridTableStyle});
            this.dataGrid.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid_Navigate);
            // 
            // dataGridTableStyle
            // 
            this.dataGridTableStyle.AlternatingBackColor = System.Drawing.Color.White;
            this.dataGridTableStyle.BackColor = System.Drawing.Color.LightYellow;
            this.dataGridTableStyle.DataGrid = this.dataGrid;
            this.dataGridTableStyle.ForeColor = System.Drawing.Color.Navy;
            this.dataGridTableStyle.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                                                                                                                 this.dataGridTextBoxColumn_id,
                                                                                                                 this.dataGridTextBoxColumn_name});
            this.dataGridTableStyle.HeaderFont = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
            this.dataGridTableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGridTableStyle.MappingName = "Balls";
            // 
            // dataGridTextBoxColumn_id
            // 
            this.dataGridTextBoxColumn_id.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
            this.dataGridTextBoxColumn_id.Format = "";
            this.dataGridTextBoxColumn_id.FormatInfo = null;
            this.dataGridTextBoxColumn_id.HeaderText = "Код";
            this.dataGridTextBoxColumn_id.MappingName = "ball_id";
            this.dataGridTextBoxColumn_id.Width = 40;
            // 
            // dataGridTextBoxColumn_name
            // 
            this.dataGridTextBoxColumn_name.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
            this.dataGridTextBoxColumn_name.Format = "";
            this.dataGridTextBoxColumn_name.FormatInfo = null;
            this.dataGridTextBoxColumn_name.HeaderText = "Наименование";
            this.dataGridTextBoxColumn_name.MappingName = "ball_name";
            this.dataGridTextBoxColumn_name.Width = 125;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(160, 208);
            this.button1.Name = "button1";
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Balls
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(242, 244);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button1,
                                                                          this.dataGrid});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "Balls";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Справочник оценок";
            this.Load += new System.EventHandler(this.Balls_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void Balls_Load(object sender, System.EventArgs e)
        {
            Config cfg = Config.Instance;
            string connectionString = cfg.connectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);
            conn.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter("select * from Balls", conn);
            ds = new DataSet();            
            
            adapter.Fill(ds, "Balls");
            dataGrid.SetDataBinding(ds, "Balls");

            
        }

        private void dataGrid_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
        {
        
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            ds.AcceptChanges();
        }
    }
}
... <<Слушаю Van Halen — One Foot Out The Door >>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.