1.直接在datagridview里面修改数据后保存到数据库 2.当datagridview第三列为空归为不合格,反之为合格,用chart饼形图表示合格与不合格的占比 3.计算出datagridview第8列为空的行数,如果超过总行数的80%并且第8列与第7列不等的行数超过了20%,Label提示不合格 4.将datagridview复选框选中行的第8列等于第6列加第七列 5.datagridview多表联动 6.如何查询数据库四个表的信息 7.如何根据四个表建立一个mysql数据库的新表
时间: 2023-06-18 14:06:02 浏览: 99
datagridview的各种常用操作方法
1. 在datagridview的CellEndEdit事件中进行保存操作:
```
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
string id = row.Cells["id"].Value.ToString();
string name = row.Cells["name"].Value.ToString();
int age = Convert.ToInt32(row.Cells["age"].Value);
string sql = "UPDATE MyTable SET name='" + name + "', age=" + age + " WHERE id='" + id + "'";
// 执行sql语句,保存修改
// ...
}
```
2. 在datagridview的CellValueChanged事件中进行饼形图的更新操作:
```
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2) // 第三列变化时
{
int qualifiedCount = 0;
int unqualifiedCount = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[2].Value == null) // 第三列为空
{
row.Cells[3].Value = "不合格";
unqualifiedCount++;
}
else
{
row.Cells[3].Value = "合格";
qualifiedCount++;
}
}
// 更新饼图
chart1.Series[0].Points.Clear();
chart1.Series[0].Points.AddXY("合格", qualifiedCount);
chart1.Series[0].Points.AddXY("不合格", unqualifiedCount);
}
}
```
3. 在某个按钮的点击事件中进行检查操作:
```
private void button1_Click(object sender, EventArgs e)
{
int emptyCount = 0;
int notEqualCount = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[7].Value == null) // 第八列为空
{
emptyCount++;
if (row.Cells[6].Value != row.Cells[7].Value) // 第八列与第七列不等
{
notEqualCount++;
}
}
}
if (emptyCount > dataGridView1.Rows.Count * 0.8 && notEqualCount > emptyCount * 0.2)
{
label1.Text = "不合格";
}
else
{
label1.Text = "合格";
}
}
```
4. 在datagridview的CellContentClick事件中进行复选框选中行的操作:
```
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0) // 复选框列
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
int sum = Convert.ToInt32(row.Cells[6].Value) + Convert.ToInt32(row.Cells[7].Value);
row.Cells[8].Value = sum;
}
}
```
5. 数据库多表联动可以使用SQL语句的JOIN操作。例如,连接两个表:
```
SELECT *
FROM Table1
JOIN Table2 ON Table1.id = Table2.id;
```
6. 根据四个表查询信息可以使用多个JOIN操作。例如,连接四个表:
```
SELECT *
FROM Table1
JOIN Table2 ON Table1.id = Table2.id
JOIN Table3 ON Table2.id = Table3.id
JOIN Table4 ON Table3.id = Table4.id;
```
7. 根据四个表建立一个mysql数据库的新表可以使用CREATE TABLE语句。例如,根据四个表创建一个新表:
```
CREATE TABLE NewTable (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10),
address VARCHAR(100)
);
INSERT INTO NewTable
SELECT Table1.id, Table1.name, Table2.age, Table3.gender, Table4.address
FROM Table1
JOIN Table2 ON Table1.id = Table2.id
JOIN Table3 ON Table2.id = Table3.id
JOIN Table4 ON Table3.id = Table4.id;
```
阅读全文