简体   繁体   中英

How to add a custom column

i want to display two columns in datagrid view . first by sql-table second is unbounded where i want some selection button which tells which row in selected.

i get the first column from table but i am not able to figure it out how to add second column.Following is my code.

namespace WindowsFormsApplication14
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string constring = @"server=.\SQLSER;database=test1;integrated security=true;";
            string sql = @"select rel.depar from rel RIGHT OUTER JOIN cust on cust.id=rel.id";
            SqlConnection con = new SqlConnection(constring);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            SqlDataReader red = cmd.ExecuteReader();

            dataGridView1.ColumnCount = 2;
            dataGridView1.Columns[0].Name = "department";
            dataGridView1.Columns[1].Name = "unboundcolumn";

            while (red.Read())
            {
                dataGridView1.Rows.Add(red["depar"]);                            
            }

            red.Close();
            con.Close();
        }
    }
}
dataGridView1.Columns.Add("myColumn", "My Column");

Update after comment:

You can add the text in the button as a second parameter to add:

dataGridView1.Rows.Add(red["depar"], "Button Text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM