怎么才能选中datagridview 中的checkbox????

deng520159 2008-12-06 01:02:38
有个datagridview第一列是checkbox,但是没有能得到绑定数据,想在绑定数据后,再一行行判断是否应该选中,但是怎么选中??下面这方法试了,不行??求教高人???????
string sql = "SELECT * FROM 课程";
SqlMethod.bindDataGridView(dataGridView1 ,sql,"课程",ConnectionCom .getConnection());
int sum = dataGridView1.Rows.Count;
StringCom.showInformation(sum.ToString());
for (int i = 0; i < sum;i++ )
{
//string courceid= dataGridView1.Rows[i].Cells[1].Value.ToString();
////StringCom.showInformation(courceid);
////StringCom.showInformation(temp.ToString());
//if (ConnectionCom.IsHaveSelectCource(stuNumber, Convert .ToInt32(courceid)))
//{

DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
checkcell.Value = true;
//}

...全文
144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
deng520159 2008-12-06
  • 打赏
  • 举报
回复
好了,还是我这方法不错,开始不是有一个没有数据的么??我生成个column放进去不就得了,后来如果选中了,看看数据中有没有,没有就插入,而没选中,也看看数据中有没有,有就删除!!!感谢大家支持!!!!!!!
namespace 学籍管理系统
{
public partial class SelectCource : Form
{
private string stuNumber;
public SelectCource(string stuNum)
{
InitializeComponent();
stuNumber = stuNum;
GP.Text = stuNum+"的选课信息";
bindDataGridView();
}
protected void bindDataGridView()
{
string sql = "SELECT * FROM 课程";
DataTable dt = SqlMethod.ReturnDataTable(ConnectionCom .getConnection (),sql,"课程");
DataColumn dc = new DataColumn("select",System.Type.GetType("System.Boolean"));
dt.Columns.Add(dc);
dc.SetOrdinal(1);
int sum = dataGridView1.Rows.Count;
//StringCom.showInformation(sum.ToString());
for (int i = 0; i < dt.Rows.Count;i++ )
{
string courceid= dt.Rows[i]["课程编号"] .ToString();
//StringCom.showInformation(courceid);
//StringCom.showInformation(temp.ToString());
if (ConnectionCom.IsHaveSelectCource(stuNumber, Convert.ToInt32(courceid)))
{
dt.Rows[i]["select"] = true;
}
else
{
dt.Rows[i]["select"] = false;
}
}
dataGridView1.DataSource = dt;
}

private void SelectCource_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
try
{
int sum = dataGridView1.Rows.Count;
for (int i = 0; i < sum; i++)
{
//StringCom.showInformation(dataGridView1.Rows[i].Cells[0].Value.ToString());
int courseid = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());
//StringCom.showInformation(this.dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString().Trim());
if (this.dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString().Trim() == "True")
{
if (!ConnectionCom.IsHaveSelectCource(stuNumber, courseid))
{
string sql = " insert into 选课(学号,课程编号) values(" + StringCom.QuoteStr(stuNumber) + "," + courseid + ")";
SqlMethod.InsertUpdateDeleteToDataBase(ConnectionCom.getConnection(), sql);
}
}
else
{
if (ConnectionCom.IsHaveSelectCource(stuNumber, courseid))
{
string sql = " delete from 选课 where 学号=" + StringCom.QuoteStr(stuNumber) + " and 课程编号=" + courseid;
SqlMethod.InsertUpdateDeleteToDataBase(ConnectionCom.getConnection(), sql);
}
}
}
StringCom.showInformation("保存成功!");
this.Dispose();
}
catch (Exception err)
{
StringCom.showInformation(err.Message);
}
}
}
}
doop2005 2008-12-06
  • 打赏
  • 举报
回复
this.dataGridView1.EndEdit();
试试
deng520159 2008-12-06
  • 打赏
  • 举报
回复
EditedFormattedValue只是个只读属性,????
deng520159 2008-12-06
  • 打赏
  • 举报
回复
上面的兄弟,主要是主怎么手动设置没有绑定数据的这个checkbox怎么让其打上钩?处于选中状态???????
qiaoguangyan86 2008-12-06
  • 打赏
  • 举报
回复
选择列在第几列这里Cells[n] n就是数字几,注意下标是从零开始的!
qiaoguangyan86 2008-12-06
  • 打赏
  • 举报
回复
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Trim() == "True")
{
}
}
试试这个行吧!
tianshangfei 2008-12-06
  • 打赏
  • 举报
回复
oo

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧