C# DataGridView 添加Button -轉

也不知道是否該應用這個控件,不過也想不出該用其他什麼控件,關鍵是俺比較菜沒什麼經驗。

要求是這樣的,用戶一次添加一個任務,這個任務有三個選項,其中兩個選項是用戶動態輸入的名稱(就象圖中bb和dd兩列),另一個選項則是一堆數據(就象qq那列),我現在要把每個任務羅列出來,不能用treeview,不能用tabcontrol,不能用xml,最好象個表格一樣清晰明朗(瘋了!)每個任務對應兩個按鈕,一個是Run,爲了跑任務,一個是Remove,爲了移除任務。

 

 

當然最開始選擇DataGridView就是爲了滿足那個“象表格一樣清晰明朗”的奇怪需求,一行對應一個任務,其次貌似DataGridView控件在.net 2.0中加入了新的特徵如DataGridViewButtonColumn啊,DataGridViewComboBoxColumn之類的東東。研究了一天才發現這兩個東東根本派不上用場,首先DataGridViewButtonColumn中的Button跟真的Button根本沒得比,既不能添加Button的Text也不好添加Click事件(應該是有方法的但還是很彆扭而且也沒研究);其次是DataGridViewComboBoxColumn,不僅外觀上不能和真正的ComboBox相提並論,而且當你選擇了其中的任一item,DataGridView就會新增一行,這跟我們的需求是完全不符合的,畢竟一個選項是不能代表一個任務的。

從網上查了很多資料,呵呵,看到一篇說可以畫個控件上去,覺得很有意思。其實我就是這麼做的。

(1)首先初始化DataGridView控件。

private void Form1_Load(object sender, EventArgs e)
{
           DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

            columnHeaderStyle.BackColor = Color.Beige;

            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);

            dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

            this.dataGridView1.Columns.Add("1", "bb");

            this.dataGridView1.Columns.Add("2", "qq");

            this.dataGridView1.Columns.Add("3", "dd");

            this.dataGridView1.Columns.Add("4", "aa");

}

(2)單擊按鈕添加一行,包括單元格和單元格的值,看似內嵌在單元格中的按鈕和下拉列表


 

private void button4_Click(object sender, EventArgs e)

{

            DataGridViewRow dr = new DataGridViewRow();

            foreach (DataGridViewColumn c in this.dataGridView1.Columns)

            {

                dr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);  //給行添加單元格

            }

dr.Cells[0].Value = "1111";

            dr.Cells[2].Value = "3333";

            this.dataGridView1.Rows.Add(dr);

            int index = this.dataGridView1.Rows.Count - 2;

 

            ComboBox com = new ComboBox();

            com.Name = "Containers" + index.ToString(); ;

            com.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            com.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

            com.Items.AddRange(new object[] {

            "1",

            "2",

            "3",

            "4"});

            com.SelectedIndex = 0;

            this.dataGridView1.Controls.Add(com);

            this.dataGridView1.Columns[1].Width = com.Width;

            com.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(1, index, true).Right) - (com.Width)), this.dataGridView1.GetCellDisplayRectangle(1, index, true).Y);

 

            Button btn1 = new Button();

            btn1.Name = "btnRun" + index.ToString(); ;

            btn1.Text = "Run";

            btn1.Click+=new EventHandler(btn1_Click);

 

            Button btn2 = new Button();

            btn2.Name = "btnRemove"+index.ToString();

            btn2.Text = "Remove";

            btn2.Click+=new EventHandler(btn2_Click);

 

            this.dataGridView1.Controls.Add(btn1);

            this.dataGridView1.Controls.Add(btn2);

            this.dataGridView1.Columns[3].Width = btn1.Width + btn2.Width + 6;

            btn1.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(3, index, true).Left)), this.dataGridView1.GetCellDisplayRectangle(3, index, true).Y);

            btn2.Location = new  System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(3, index, true).Right-1) - (btn2.Width)), this.dataGridView1.GetCellDisplayRectangle(3, index, true).Y);        

}

(3)爲(2)中生成的Run按鈕和Remove按鈕添加單擊事件處理程序

public void btn1_Click(object sender, EventArgs e)

{

            this.richTextBox1.Text = "";

            Button btn = (Button)(sender);

            //這個btn的name是btnRun打頭的

            string suffix = btn.Name.ToString().Substring(6); //後邊那個號,相當於index的string

            Control c = findControlByName(suffix);  

            if (c != null)

            {

                ComboBox com = (ComboBox)(c);

         //Control ctl1 = this.dataGridView1.Controls["Containers" + i.ToString()];

        //ComboBox com = (ComboBox)ctl1;  其實這樣寫更簡單點

                for (int i = 0; i < com.Items.Count; i++)

                {

                    this.richTextBox1.Text += com.Items[i].ToString() + "\n";

                }

            }

}

 

public void btn2_Click(object sender, EventArgs e)  

{

  

      int RowCount = this.dataGridView1.Rows.Count;

      Button btn = (Button)(sender);

            //這個btn的name是btnRemove打頭的

            string suffix = btn.Name.ToString().Substring(9);  //後邊那個號,相當於index的string

            this.dataGridView1.Controls.RemoveByKey(btn.Name);

            this.dataGridView1.Controls.RemoveByKey("btnRun" + suffix);

            this.dataGridView1.Controls.RemoveByKey("Containers" + suffix);

            int index = Convert.ToInt32(suffix);

            this.dataGridView1.Rows.RemoveAt(index);

       if (index < RowCount - 2)

            {

                for (int i = index + 1; i < RowCount - 1; i++)  

                {

                    Control ctl1 = this.dataGridView1.Controls["Containers" + i.ToString()];

                    Control ctl2 = this.dataGridView1.Controls["btnRun" + i.ToString()];

                    Control ctl3 = this.dataGridView1.Controls["btnRemove" + i.ToString()];

                    ComboBox com = (ComboBox)ctl1;

                    Button btnRun = (Button)ctl2;

                    Button btnRemove = (Button)ctl3;

                    //上移一格,單元格Height位4,Button的Height爲23

                    com.Location = new System.Drawing.Point(com.Location.X, com.Location.Y - 23);

                    btnRun.Location = new System.Drawing.Point(btnRun.Location.X, btnRun.Location.Y - 23);

                    btnRemove.Location = new System.Drawing.Point(btnRemove.Location.X, btnRemove.Location.Y - 23);

                    //改名字的後綴

                    int j=i-1;

                    com.Name = "Containers" + j.ToString();

                    btnRun.Name = "btnRun" + j.ToString();

                    btnRemove.Name = "btnRemove" + j.ToString();

                }

            }

}

 

(4)btn1_Click處理中用到的函數findControlByName()

public Control findControlByName(string suffix)

{

            foreach (System.Windows.Forms.Control c in this.dataGridView1.Controls)

            {

                if (c.Name == "Containers" + suffix)

                    return c;

            }

            return null;

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章