有个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;
//}
<span class="STYLE8" >选择:</span>
<span id="select1" class="STYLE8" onclick="SettingChecked('selectAll');BackColor();this.style['color']='#33AA3F'">全部</span> -
<span id="select2" class="STYLE8" onclick="SettingChecked();BackColor();this.style['color']='#33AA3F'"> 反选 </span>-
<span id="select3" class="STYLE8" onclick="NoCheckSelect();BackColor();this.style['color']='#33AA3F'"> 不选</span>
<script type="text/javascript" src="../JS/prototype.js"></script>
<script language="javascript" type="text/javascript">
//设置 CheckBox 的选择状态js反选 全选 多选框 兼容ie和firefox版本
function SettingChecked(isCked)
{
var ck=document.getElementsByTagName("input");//意思是找到页面上所有 <input > 节点
for(var i=0 ;i<ck.length;i++)
{
if(ck[i].type=="checkbox")
{
ck[i].checked=(isCked=="selectAll")? true :(!ck[i].checked);
}
}
}
//判断选中提示。
function DelAllItem(id,evt)
{
if(CkeckedTolNum(id)==0)
{
alert('<%=FSSoft.Web.UI.Language.Msg("00005") %>');
return false;}
else
{return window.confirm('<%=FSSoft.Web.UI.Language.Msg("00008") %>');}
}
function CkeckedTolNum(cid)
{
var num =0;
var cks =document.getElementsByTagName("input");
for(var i=0;i<cks.length;i++)
{
//cks[i].id.indexOf(cid) = 0 时,说明 是以 cid 开头的 ID
if(cks[i].type=="checkbox" && cks[i].checked && cks[i].id.indexOf(cid)>=0)
{num ++;}
}
return num;
}
function NoCheckSelect()//不选
{
var cks =document.getElementsByTagName("input");
for(var i = 0; i < cks.length ; i++)
{
if(cks[i].type.toLowerCase() =="checkbox" && cks[i].checked)
{
//如果选择框选中则清空
if(cks[i].checked.toString() == "true"){cks[i].checked = false;}
}
}
}a
function BackColor()//点击字体颜色变化
{
var q = 5;for(i=1 ; i<5;i++)
{if($("select"+i)!=null){$("select"+i).style.color='';}}
}
</script> <asp:CheckBox ID="single_Chk" name="selectAll" runat="server" />
你在DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
后面加一个
MessageBox.Show(dataGridView1.Rows[i].Cells[i].ValueType.ToString());看看这个单元格的值是不是Boolean类型的,
{
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);
}
}
}
}