DataGridView1 結合 RadioButton

dataGridView1

看完Puma 大大的範例也試了一下

只是呈現出來樣式大小似乎有點不同,不過功能面相同...

參考http://www.dotblogs.com.tw/puma/archive/2008/11/10/5943.aspx

這架構都差不多,只差在XY軸的改變而已,不然核心都相同

 

 

image


 private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select bookid as ID, book_name AS 書名, recommend as 評價 from book";
            SqlConnection co = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["test1"].ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText =sql;
            SqlDataAdapter ad = new SqlDataAdapter(cmd.CommandText,co.ConnectionString.ToString());
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dataGridView1.DataSource = dt;
        
        }
        private bool _CheckChange = false;
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "評價")
            {
                Rectangle r = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
                r = this.dataGridView1.RectangleToScreen(r);
                this.radioButton1.Location = this.RectangleToClient(r).Location;
                this.radioButton1.Size = r.Size;
                this._CheckChange = true;
                this.radioButton1.Text = "好";
               
                this.radioButton1.Visible = true;
                //取X Y軸的位置
                int  xa = int.Parse(this.RectangleToClient(r).Location.X.ToString());
                int ya = int.Parse(this.RectangleToClient(r).Location.Y.ToString());
                
                this.radioButton2.Location = new System.Drawing.Point(xa+35, ya);//Xa+ 的原因是兩個radioButton初始位置一樣,Y軸就不用變了同行
                this.radioButton2.Size = r.Size;   
                this.radioButton2.Text = "壞";
                this._CheckChange = false;
                this.radioButton2.Visible = true;

            }
            else
            {
                this.radioButton1.Visible = false;
                this.radioButton2.Visible = false;
            }
            if (this.dataGridView1.CurrentCell.Value.ToString() == "好")
            {
                this.radioButton1.Checked = true;
            }
            if (this.dataGridView1.CurrentCell.Value.ToString() == "壞")
            {
                this.radioButton2.Checked = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (_CheckChange) return;
            this.dataGridView1.CurrentCell.Value = this.radioButton1.Text;
         
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            this.radioButton1.Visible = false;
            this.radioButton2.Visible = false;
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            this.radioButton1.Visible = false;
            this.radioButton2.Visible = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (_CheckChange) return;
            this.dataGridView1.CurrentCell.Value = this.radioButton2.Text;

        }

 

 

image

 

 

 

 

大家一起加入blogads 賺零用錢!!