-3

I have actually a datagridview loaded with sql query. i want to add a new column with some rows without clean all the data.

The values which I will insert in the rows are string in a list. And this list is loaded by an sql query made by the first column of the datagridview already loaded.

So i wanted to do basically :

Seals_DataGridView.Columns.Add("column_Type", "Title Column");

        Seals_DataGridView.Rows.Add("GOGO"); 

But obviously this one doesn't work

Anyone have and idea to help me ?

Thanks

BiRoy
  • 17
  • 6
  • What doesn't work about it? I would imagine you just want to loop through the rows and set the value for the new column – Charlieface Jan 18 '21 at 14:52
  • 1
    Does this answer your question? [Programmatically add new column to DataGridView](https://stackoverflow.com/questions/5524075/programmatically-add-new-column-to-datagridview) and [How to add a new row to datagridview programmatically](https://stackoverflow.com/questions/10063770/how-to-add-a-new-row-to-datagridview-programmatically) –  Jan 18 '21 at 14:53
  • I already seen this post, honestly, I'm not really friendly with the DataSource aspect, that's why I used SQL query for my datagridview. I don't really understand how to fix my problem. The objective is to just to add a new column and yes, after that I'll loop through the rows. Add the column without clean my data it's my main issue – BiRoy Jan 18 '21 at 14:59

1 Answers1

2

If you want to add rows from code try using DataTable and DataGridViewDataSource

string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }
            }

and other best way is to use Grapically

Comming to your problem

Seals_DataGridView.Columns.Add("column_Type", "Title Column");
        Seals_DataGridView.Rows.Add("GOGO"); 

Code you given worked perfectly for me i don't know what the issue with your code you have to elaborate

sai kiran
  • 389
  • 2
  • 10
  • Thanks for the answer, and if I'll add the new column by the datagridview's property, this one will don't delete my data loaded by the datasource? – BiRoy Jan 18 '21 at 15:09
  • The error is : "Cannot add lines programming to DataGridView line collection when control is data bound". But I can see behind the error the new column – BiRoy Jan 18 '21 at 15:13
  • I'm finding the problem with your answer, but the main issue was resolved with your advice, thanks – BiRoy Jan 18 '21 at 15:20
  • Your most welcome that your problem resolved – sai kiran Jan 18 '21 at 15:40