这段时间需要使用DataGridView,故来总结一下
首先需要做的是连接数据库,获得数据
string str_con = @"Data Source=.;Integrated Security=sspi;Initial Catalog=testDB;";
string sql = "select * from Person";
SqlConnection conn = new SqlConnection(str_con);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds, "Person");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Person";
conn.Close();
这边就不多说了,我的项目里是从类里取得数据
dataGridView1.DataSource = 类名;
(1)修改行头和宽度
dataGridView1.Columns[0].HeaderText = "类别";
dataGridView1.Columns[1].HeaderText = "开始时间";
dataGridView1.Columns[2].HeaderText = "结束时间";
dataGridView1.Columns[0].Width = 80;
dataGridView1.Columns[1].Width = 150;
dataGridView1.Columns[2].Width = 150;
(2)添加CheckBox选择框
DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
{
column1.HeaderText = "选择";
column1.Name = "check1";
column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
column1.FlatStyle = FlatStyle.Standard;
//显示选择框的三种状态
column1.ThreeState = true;
}
dataGridView1.Columns.Insert(0, column1);//插入到最前面
这里说明DataGridView可以添加6中按钮控件,除了CheckBox还有Button、TextBox、Image、Link和ComboBox,用法都是一样的
(3)隐藏新增行和列头
dataGridView1.RowHeadersVisible = false;
dataGridView1.AllowUserToAddRows = false;
(4)设置字体居中显示
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
(5)设定包括Header和所有单元格的列宽和行高自动调整
GV_Visit.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
GV_Visit.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
(6)设置行首和左上角的文字
dataGridView1.Rows[0].HeaderCell.Value = "第1行";
/dataGridView1.TopLeftHeaderCell.Value = "左上角";
(7)取得选定的行、列、单元格
//选定的单元格
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.Index);
}
foreach (DataGridViewColumn c in dataGridView1.SelectedColumns)
{
listBox1.Items.Add("选定的列是:" + c.Index);
}
(8)删除行、列
//删除指定行
dataGridView1.Columns.RemoveAt(0);
dataGridView1.Columns.Remove("Name");
//删除指定列
dataGridView1.Rows.RemoveAt(0);
//删除多行
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
if (r.IsNewRow == false)
{
dataGridView1.Rows.Remove(r);
}
}
(9)因为我的项目里新增了CheckBox选择框,我想实现单选需要在CellContentClick事件中说明
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int count = Convert.ToInt16(dataGridView1.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["check1"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
checkCell.Value = false;
}
else
{
continue;
}
}
}
(10)获取到选择的行的数据
string IData = "";
int count = Convert.ToInt32(dataGridView1.Rows.Count.ToString());//获取到总行数
for (int i = 0; i < count; i++)
{
//如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
dataGridView1.EndEdit();
//获取到选择的行数
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)GV_Visit.Rows[i].Cells["check1"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true)//查找被选择的数据行
{
for (int j = 1; j < dataGridView1.ColumnCount; j++)
{
//从 DataGridView中获取某行某列的数据项
string data = dataGridView1.Rows[i].Cells[j].Value.ToString().Trim();
IData += data + ",";
}
//如果只需要取某一列的值,那就不需要遍历列了
//string data = dataGridView1.Rows[i].Cells[列数].Value.ToString().Trim();
}
}
if (!string.IsNullOrEmpty(IData))
{
MessageBox.Show(IData.TrimEnd(','));
}
(11)获得列名为“**”的列
因为在项目中,经常有关于性别或其他的,在数据库中储存为0/1,在页面显示需要表示为男/女或真/假之类的
int num= dataGridView1.Columns["Gender"].Index;
//获得列名叫Gender的列的索引
//通过遍历所有的行,值为0则是男反之则为女
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[num].Value.ToString().Trim() == "0")
{
dataGridView1.Rows[i].Cells[num].Value = "男";
}
else
{
dataGridView1.Rows[i].Cells[num].Value = "女";
}
}