可以在窗口应用程序中的gridview中添加复选框列。 [英] Can have possibility to add a checkbox column in gridview in window application.
本文介绍了可以在窗口应用程序中的gridview中添加复选框列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在gridview中添加一个复选框列,我们可以选中并取消选中这些复选框,并根据可以在数据库中更新。
请帮帮忙。我被困在这一部分。
提前谢谢。
I have requirement to add a checkbox column in gridview and we can check and uncheck these checkbox and according to that can update in database.
Please help in out. I am stuck in this portion.
Thanks in advance.
推荐答案
将DataGridViewCheckBoxColumn列添加到gridview并处理CellClick事件
Add DataGridViewCheckBoxColumn column to gridview and handle CellClick event
if (dataGridRow.Cells["YourCheckboxColumn"].Value != null && (bool)dataGridRow.Cells["YourCheckboxColumn"].Value)
{
// Checked
}
else if (dataGridRow.Cells["YourCheckboxColumn"].Value == null)
{
// Unchecked
}
调用db update方法然后。
Call your db update method then.
您的问题是关于DataGridView。 GridView适用于asp.net。
参考:
1. How-create-check-box-column-datagridview [ ^ ]
2. insert-checked-rows-records-of-datagridview-into-database.aspx [ ^ ]
对于2,您应该将sql代码更改为参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]
You question is about DataGridView. GridView is for asp.net.
Refer:
1. How-create-check-box-column-datagridview[^]
2. insert-checked-rows-records-of-datagridview-into-database.aspx[^]
For 2, you should change the sql code to Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
//it will executed on click of any cell in gridview
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//it will check the specific column
if (e.ColumnIndex == dataGridView1.Columns["status"].Index)
{
dataGridView1.Rows[e.RowIndex].Cells["status"].Value = !
(bool)dataGridView1.Rows[e.RowIndex].Cells["status"].Value;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
String str = "update DemoTable set Status=" + status +" where Sln=" +
dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
//它已经过测试。如果它会对你有帮助,那么点击接受解决方案按钮
让我知道。
//it is tested one.If it will help you ,then click on the accept solution button
let me know.
这篇关于可以在窗口应用程序中的gridview中添加复选框列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文