简体   繁体   English

如何添加自定义列

[英]How to add a custom column

i want to display two columns in datagrid view . 我想在datagrid视图中显示两列。 first by sql-table second is unbounded where i want some selection button which tells which row in selected. 首先通过sql-table第二个是无限的,我想要一些选择按钮,告诉我选择哪一行。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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