visual studio控件datagridview的操作

                             网上关于datagridview连接数据库的操作已经很多,在此不做过多的赘述,下面是一些用来当表格使用时的具体操作


            1.  代码创建datagridview 传入数组参数确定字段名

public System.Windows.Forms.DataGridView cretedatagridview( int columns, string[] column_naems) 
        {
      
        List cos = new List();
        for (int i = 0; i < columns; i++) 
        {
            System.Windows.Forms.DataGridViewTextBoxColumn Column= new System.Windows.Forms.DataGridViewTextBoxColumn();
            Column.HeaderText = column_naems[i];
            Column.Name = "Column3";
            Column.Width = 120;
            cos.Add(Column);
        }
            

        System.Windows.Forms.DataGridView dataGridView= new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(dataGridView)).BeginInit();
        dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top |                                          System.Windows.Forms.AnchorStyles.Bottom)
         | System.Windows.Forms.AnchorStyles.Left)
         | System.Windows.Forms.AnchorStyles.Right)));
        dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        dataGridView.Columns.AddRange(cos.ToArray());
        dataGridView.Location = new System.Drawing.Point(10, 79);
        dataGridView.Name = "dataGridView";
        dataGridView.RowTemplate.Height = 23;
        dataGridView.Size = new System.Drawing.Size(793, 378);
        dataGridView.TabIndex = 0;
        dataGridView.Visible = true;
        ((System.ComponentModel.ISupportInitialize)( dataGridView)).EndInit();
        return dataGridView;
           }

            2.     定焦点,读内容,写内容
             this.dataGridView1.CurrentCell = dataGridView1[0, 0];
            MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
            this.dataGridView1.CurrentCell = dataGridView1[1, 0];
            dataGridView1.CurrentCell.Value = "123";

            3.使datagridview显示固定的行号

            //该句话是重点
            this.dataGridView1.Rows.Add(5); 

            4.合并datagridview的单元格(由于c#的类库未提供单元格的合并方法,所以现在通用的做法是采用datagridview的cellpainting方法重画单元格,掩藏其背景色,将单元格相挨且内容相同的单元格合并)

            


                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
                            //绘制值
                            if (e.Value != null)
                            {
                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                    Brushes.Crimson, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 2, StringFormat.GenericDefault);
                            }
                        }
                        //右侧的线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom-1);

                        e.Handled = true;
                    }
                }
            }

            //横向合并
            if (this.dataGridView1.Columns["Column2"].Index == e.ColumnIndex && e.RowIndex >= 0)
            {

                using (
                    Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 擦除原单元格背景
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                      
                        if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                        {

                            //右侧的线
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                                e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            //绘制值
                            if (e.Value != null)
                            {
                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                    Brushes.Crimson, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 2, StringFormat.GenericDefault);
                            }
                        }
                        else
                        {
                            e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds);
                            this.nextcol = e.ColumnIndex +1;
                        }
                     
                        //下边缘的线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                    e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        e.Handled = true;
                    }
                }

            }
           // MessageBox.Show("haha,I am here");
        }

你可能感兴趣的:(c#心得,c#,visual,studio,datageidview,合并,赋值)