有个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;
//}
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;
//}
解决方案 »
- 表格控件datagridview中第一列不能输入相同的数据,
- C#数据库问题 求各位高人解决
- 帮忙加一判断语句
- 急求!谁会通过button把Excel中的数据导入到DataGrid中???
- web form与win form中,请问在insert插入字符串sql语句中,怎么插入 '单引号,不出错?有什么简便的方法吗?
- web服务器被黑,现在iis日志无法生成,怎么恢复?
- .NET Framework 求助 (C# 开发应用程序)
- 菜鸟问题:关于菜单的显示问题。
- 特菜的问题:如何使窗体与其它窗体相关联?
- 请问,怎样用C# 实现类似于PB中的 Response 窗口
- 还是INI读的问题
- 一个简单的问题,请老师指点。
{
if (this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Trim() == "True")
{
}
}
试试这个行吧!
试试
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);
}
}
}
}