簡體   English   中英

如何添加自定義列

[英]How to add a custom column

我想在datagrid視圖中顯示兩列。 首先通過sql-table第二個是無限的,我想要一些選擇按鈕,告訴我選擇哪一行。

我從表中得到第一列,但我無法弄清楚如何添加第二列。以下是我的代碼。

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

評論后更新:

您可以將按鈕中的文本添加為​​要添加的第二個參數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM