Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have folowing problem: i have datagrid with columns created by grid editor. When i try to fill this fields from my database information is filled after the existing columns.

What I have tried:

I try to fill my grid columns with correct data
Posted
Updated 19-Apr-16 22:02pm
Comments
BulletVictim 19-Apr-16 8:03am    
If I'm understanding your problem correctly, you need to set datagrid.autogenerateColumns = false;
Should keep it from generating the columns from the database and populate the exiting columns with the data.
Member 12470237 19-Apr-16 9:08am    
I try this but does't work when i set datagrid.autogenerateColumns = false datagrid stay empty. Datagrid columns names are the same like database columns names

You can let the datagrid autogenerate the columns.
C#
SqlCommand cm = new SqlCommand("Select * from table1", con);
 SqlDataReader dr;
 dr = cm.ExecuteReader();
 DataTable dataTable = new DataTable();
 dataTable.Load(dr);
 dataGridView1.DataSource = dataTable;

Here is an excellent article with datagrid examples on CodeProject: A Detailed Data Binding Tutorial[^]
 
Share this answer
 
v2
Comments
Member 12470237 20-Apr-16 2:33am    
My code is exactly what you propose to me. When i let to autogenerate the columns, fist colums, where i want to flll data from database are empty. Database colums with data are imported after this columns
phil.o 21-Apr-16 3:38am    
Then delete the first/hand-made columns, and let the system populate your whole grid with relevant columns.
Or am I missing something?
Member 12470237 25-Apr-16 7:23am    
This solution work. Thanks :)
RickZeeland 25-Apr-16 8:44am    
Then you can mark the solution as "Answer".
Strange that solution1 does not work for you, please try again from scratch, maybe you have set some properties that cause problems.
Here is my complete example for SQL Server Express with Windows Authentication, it works without any problems on my Windows 7 notebook with SQL Server 2008 R2 and .NET 4.5.
The Chinook database is an example database which can be downloaded from CodePlex: https://chinookdatabase.codeplex.com/
C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form6 : Form
    {
        string connectionString = @"Integrated Security=SSPI;Persist Security Info=False;Data Source=(local)\SQLEXPRESS;Initial Catalog=Chinook";
        DataTable dataTable;

        public Form6()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool result;

            using (var con = new SqlConnection(connectionString))
            {
                con.Open();
                result = con.State == System.Data.ConnectionState.Open;

                if (!result)
                {
                    MessageBox.Show("Could not connect !");
                }
                else
                {
                    Console.WriteLine("SQL Server Express " + con.ServerVersion);
                    SqlCommand cm = new SqlCommand("Select * from Artist", con);
                    SqlDataReader dr;
                    dr = cm.ExecuteReader();
                    dataTable = new DataTable();
                    dataTable.Load(dr);
                    dataGridView1.DataSource = dataTable;
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataTable.Rows.Count; i++)
            {
                string query = string.Empty;
                int result;

                switch (this.dataTable.Rows[i].RowState)
                {
                    case DataRowState.Added:
                        Debug.Print("Row " + i + "  state = added");
                        query = "INSERT INTO Artist (ArtistId, Name) VALUES(@ArtistId, @Name)";
                        break;
                    case DataRowState.Deleted:
                        Debug.Print("Row " + i + "  state = deleted");
                        break;
                    case DataRowState.Detached:
                        Debug.Print("Row " + i + "  state = detached");
                        break;
                    case DataRowState.Modified:
                        Debug.Print("Row " + i + "  state = modified");
                        query = "UPDATE Artist SET Name=@Name WHERE ArtistId=@ArtistId";
                        break;
                    //case DataRowState.Unchanged:
                    //    break;
                }

                // Execute parameterized query.
                if (!string.IsNullOrEmpty(query))
                {
                    using (var con = new SqlConnection(connectionString))
                    {
                        con.Open();

                        using (SqlCommand command = new SqlCommand(query, con))
                        {
                            Debug.Print("ArtistId = " + this.dataTable.Rows[i].ItemArray[0]);
                            Debug.Print("Name     = " + this.dataTable.Rows[i].ItemArray[1]);
                            command.Parameters.AddWithValue("@ArtistId", this.dataTable.Rows[i].ItemArray[0]);
                            command.Parameters.AddWithValue("@Name", this.dataTable.Rows[i].ItemArray[1]);
                            result = command.ExecuteNonQuery();
                            Debug.Print("result = " + result);
                        }
                    }
                }
            }
        }

    }
}
 
Share this answer
 
v2
I find the solution ...I had to clean datagrid before load data
C#
dataGridView1.Columns.Clear();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900