C#中DataGridView的使用
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using (sqlConnection cnn = new SqlConnection(strConn))
{
try
{
cnn.Open();
bConn = true;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
bConn = false;
}
dataGridView1.Columns[1].FillWeight = 15;
dataGridView1.Columns[2].FillWeight = 15;
dataGridView1.Columns[3].FillWeight = 8;
dataGridView1.Columns[4].FillWeight = 50;
i += 2;
}
}
dataGridView1.Columns[5].FillWeight = 4;
//设置标题文字
dataGridView1.Columns[0].HeaderText = "A";
dataGridView1.Columns[1].HeaderText = "B";
dataGridView1.Columns[2].HeaderText = "C";
{
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[e.RowIndex].Selected = true; //选中
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];//选中单元
C#中DataGridView的使用
1.首先,连接数据库
Copy code
public void Connect()
在C#中,使用DataGri据库
Copy code
public void Connect()
{
string strConn = string.Format("Data Source = IP;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DataBaseName;User ID = UserID;");
SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSql, cnn);
myDataAdapter.Fill(dataset); //这句跟下面的顺序不能颠倒
dataGridView1.DataSource = dataset.Tables[0];//填充
dataGridView1.Columns[3].HeaderText = "D";
dataGridView1.Columns[4].HeaderText = "E";
dataGridView1.Columns[5].HeaderText = "F";
5.设置显示时候相隔颜色
Copy code
//颜色设置,相隔行颜色不同
public void SetDataGridColor(int nCount)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; )
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
}
}
2.构造SQL语句去数据库查询,并奖结果放到DataGridView控件
Copy code
string strSql = string.Format("select * from TableName where ID < 50 order by ID");
DataSet dataset = new DataSet();
3.添加DataGridView控件的右键菜单
Copy code
//在CellMouseClick里操作
private void DataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
DataGridRightMenu.Show(MousePosition.X, MousePosition.Y); //在点击处显示右键菜单
}
}
}
4.对DataGridView的样式进行设置
Copy code
//设置可以根据自己需要来进行
//设置列宽
dataGridView1.Columns[0].FillWeight = 8;
{
try
{
cnn.Open();
bConn = true;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
bConn = false;
}
dataGridView1.Columns[1].FillWeight = 15;
dataGridView1.Columns[2].FillWeight = 15;
dataGridView1.Columns[3].FillWeight = 8;
dataGridView1.Columns[4].FillWeight = 50;
i += 2;
}
}
dataGridView1.Columns[5].FillWeight = 4;
//设置标题文字
dataGridView1.Columns[0].HeaderText = "A";
dataGridView1.Columns[1].HeaderText = "B";
dataGridView1.Columns[2].HeaderText = "C";
{
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[e.RowIndex].Selected = true; //选中
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];//选中单元
C#中DataGridView的使用
1.首先,连接数据库
Copy code
public void Connect()
在C#中,使用DataGri据库
Copy code
public void Connect()
{
string strConn = string.Format("Data Source = IP;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DataBaseName;User ID = UserID;");
SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSql, cnn);
myDataAdapter.Fill(dataset); //这句跟下面的顺序不能颠倒
dataGridView1.DataSource = dataset.Tables[0];//填充
dataGridView1.Columns[3].HeaderText = "D";
dataGridView1.Columns[4].HeaderText = "E";
dataGridView1.Columns[5].HeaderText = "F";
5.设置显示时候相隔颜色
Copy code
//颜色设置,相隔行颜色不同
public void SetDataGridColor(int nCount)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; )
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
}
}
2.构造SQL语句去数据库查询,并奖结果放到DataGridView控件
Copy code
string strSql = string.Format("select * from TableName where ID < 50 order by ID");
DataSet dataset = new DataSet();
3.添加DataGridView控件的右键菜单
Copy code
//在CellMouseClick里操作
private void DataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
DataGridRightMenu.Show(MousePosition.X, MousePosition.Y); //在点击处显示右键菜单
}
}
}
4.对DataGridView的样式进行设置
Copy code
//设置可以根据自己需要来进行
//设置列宽
dataGridView1.Columns[0].FillWeight = 8;