C#关于DataGridView控件的使用总结【总结篇】

C#关于DataGridView控件的使用总结

【请按照换行进行对应代码段】——Frank_chen


DataGridView常用方法:
dataGridView1.ReadOnly = true;//全部单元格只读
dataGridView1.Columns[1].ReadOnly = true;///指定单元格设置只读(列)第一列
dataGridView1.Rows[2].ReadOnly = true;///指定单元格设置只读 (行)第二行
dataGridView1[1, 2].ReadOnly = true;///指定单元格设置只读 (第1行,第2列)利用坐标
dataGridView1.AllowUserToAddRows = false;//禁止用户追加行

dataGridView1.AllowUserToDeleteRows = true;//允许用户删除行操作
//提示是否删除指定行数据
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
   DialogResult diaR = MessageBox.Show("删除该行吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (diaR == DialogResult.OK)
            {
                e.Cancel = false;
            }
}
//提示删除了哪一行数据
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            messageBoxCS.AppendFormat("{0} = {1}", "行号为", e.Row);
            messageBoxCS.AppendLine();
            DialogResult diaR = MessageBox.Show("删除了" + messageBoxCS.ToString(), "确认");
        }
//删除选定的多行
   foreach(  DataGridViewRow r in dataGridView1.SelectedRows)
 {      if (r.IsNewRow == false)    {   ataGridView1.Rows.Remove(r);     }      
}
取得选定的行、列、单元格
//选定的单元格
            foreach (DataGridViewCell c in dataGridView1.SelectedCells)
            {
                string cr = string.Format("{0},{1}", c.ColumnIndex, c.RowIndex);
                listBox1.Items.Add("选定的单元格位置是:" + cr);
      }
//选定的行/列
            foreach (DataGridViewRow c in dataGridView1.SelectedRows)
            {
                listBox1.Items.Add("选定的行是:" + c.RowIndex);
     }
foreach (DataGridViewColumn c in dataGridView1.SelectedColumns)
            {
                listBox1.Items.Add("选定的列是:" + c.ColumnIndex);
     }
//指定选定单元格
         dataGridView1[0, 0].Selected = true;
         dataGridView1.Rows[0].Selected = true;
          dataGridView1.Columns[0].Selected = true;
//设置行内容
            dataGridView1.Rows[0].HeaderCell.Value = "第1行";
//利用数组添加添加数据时,类型都是默认的字符串类型
            string name = "Jim";
            string gender = "男";
            string age = "18";

            //将上面三个变量合成一个数组

            string[] row = { name, gender, age };
            //给dataGridView1控件添加数据

            dataGridView1.Rows.Add(row);//添加数据

/

/左上角的文字
            dataGridView1.TopLeftHeaderCell.Value = "左上角";
//列中显示选择框CheckBox
            DataGridViewCheckBoxColumn column1= new DataGridViewCheckBoxColumn();
           {
                column1.HeaderText = "选择框";
                column1.Name = "checkbox";
                column1.AutoSizeMode =
                DataGridViewAutoSizeColumnMode.DisplayedCells;
                column1.FlatStyle = FlatStyle.Standard;

     //显示选择框的三种状态
                 column1.ThreeState = true;
            }
            dataGridView1.Columns.Add(column1);
//显示按钮控件
            DataGridViewButtonColumn col=new DataGridViewButtonColumn();
            col.Name="Button";
            col.UseColumnTextForButtonValue=true;
            col.Text = "按钮";
            dataGridView1.Columns.Add(col);
触发的事件为:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name=="Button")
            {
                MessageBox.Show("触发了按钮");
            }
 }

单元格列显示图像的是:DataGridViewImageColumn

//显示图像
            DataGridViewImageColumn dgvI=new DataGridViewImageColumn();
            dgvI.Name="Image";
            dgvI.ValuesAreIcons=false;
            dgvI.Image = new Bitmap("c://windows//Blue Lace 16.bmp");
            dgvI.ImageLayout=DataGridViewImageCellLayout.Zoom;
            dgvI.Description="测试的图片";
            dataGridView1.Columns.Add(dgvI);
    dataGridView1["Image", 0].Value = new Bitmap("c://windows//Blue Lace 16.bmp");
//获取总行数:
dataGridView1.Rows.Count;
获取当前选中行索引:
int i = this.dataGridView1.CurrentRow.Index;
获取当前选中列索引:
int j = this.dataGridView1.CurrentCell.ColumnIndex;

你可能感兴趣的:(c#)