Click here to Skip to main content
15,845,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added database table data to DataGridView in the C# Windows application form. I want to add one more column to it with Edit Button in each row. How can I do it with code?

What I have tried:

public Form1()
       {
           InitializeComponent();
           SqlConnection con = new SqlConnection(@"Server= DESKTOP-AK4NH6J\SQLEXPRESS;Database=Interview;Trusted_connection=True");
           con.Open();
           SqlCommand cmd = new SqlCommand("select * from Person", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           dt.Columns.Add("Edit", typeof(string));
           con.Close();
           dataGridView1.DataSource = dt;
       }
Posted
Updated 15-Dec-22 21:22pm

1 solution

Because you are using a Datatable as the DataSource for your DGV, you can't add a column to the DGV itself - instead, you add it to the DataTable that underlies it and supplies the data: Adding Columns to a DataTable - ADO.NET | Microsoft Learn[^]
 
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