///让所有列都显示出来
///
将最后一列的 AutoSizeMode 属性设置为 Fill,并对其他列使用其他调整大小选项。如果其他列使用了过多的可用空间,可设置最后一列的 MinimumWidth属性。
设定背景颜色 ------------交替行样式
this.dataGridView2.RowsDefaultCellStyle.BackColor = Color.Bisque; //默认样式
this.dataGridView2.AlternatingRowsDefaultCellStyle.BackColor =Color.Beige; //奇数样式 ---形成奇偶
冻结l列 -----------效果移动横条时他不随着移动;
单击 DataGridView控件右上角的智能标记标志符号 ( ),然后选择“编辑列“;从“选定的列”列表中选择一列。 在“列属性”网格中,将 Frozen属性设置为 true。
设定数据显示格式
在“列属性”网格中,单击 DefaultCellStyle属性旁的省略号按钮 ( )。“CellStyle 生成器”对话框出现。Format
///
///不让某列排序
///
NotSortable
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
///
///转移到我们想要的哪一行
///
dataGridView1.CurrentCell=dataGridView1.Rows[1].Cells[1];
///
/// 常用事件处理
///
//数据解析 -----如数值解析文字 ;图片列;简单的密码显示
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["id"].Index ) //数字解析;同时可以完成图片解析
{
int cc=Convert.ToInt32(e.Value);
if (cc>2)
{
e.Value = "可用";
e.CellStyle.ForeColor = Color.Red;
}
else
{
e.Value = "不可用";
e.CellStyle.ForeColor = Color.Green;
}
}
///显示密码
///保存值的列隐藏,用一列显示****
///pwd列隐藏 realvalue显示*
if (dataGridView1.Columns[e.ColumnIndex].Name == "realValue" && dataGridView1.Rows
[e.RowIndex].Cells["pwd"].Value != null && dataGridView1.Rows[e.RowIndex].Cells
["pwd"].Value.ToString().Length > 0)
{
e.Value = new string('*', dataGridView1.Rows[e.RowIndex].Cells
["pwd"].Value.ToString().Length);
}
}
///
///在输入框中输入插叙条件 查找我们所要的数据,进一步检索
///dataGridView1.DataSource = nowData;
{
string sql = textBox1.Text;
//string sql = "sorting=3";
DataTable newdt = new DataTable();
newdt = nowData.Clone();
DataRow[] drs = nowData.Select(sql);
for (int i = 0; i < drs.Length; i++)
{
newdt.ImportRow((DataRow)drs[i]); //这个很重要,不然一直提示该行已经存在表中
}
nowData.Clear();
nowData = newdt;
dataGridView1.DataSource = nowData;
}
//将dataGridView里面的行锁定,即鼠标移到行与行交界处,不会出现双向箭头
dataGridView1.AllowUserToResizeRows= false;