求救,为什么显示的不是我选中那行的数据值?
DataGrid显示数据,有一个checkbox控件,通过它来选中.然后点击按钮触发Button1_Click事件,用Label10.Text显示多勾选那些的主键ID,但是不管我勾选哪一行,显示的总是最后一行的ID.是不是我哪里弄错了,下面是触发Button1_Click事件的代码:
void Button1_Click(Object sender, EventArgs e) {
try
{
string SelectedString = "";
foreach(DataGridItem data in MyDataGrid.Items)
{
for(int i=0;i<data.Cells[1].Controls.Count;i++)
{
if(data.Cells[1].Controls[i].GetType().ToString()=="System.Web.UI.WebControls.CheckBox")
{
Label10.Text=MyDataGrid.DataKeys[data.ItemIndex].ToString();//显示勾选行数据
if(((CheckBox)data.Cells[1].Controls[i]).Checked==true)
{
//总是运行不到这里,可是我有勾选啊,为什么就总是false呢?
SelectedString += MyDataGrid.DataKeys[data.ItemIndex].ToString() + ",";
}
}
}
}
下面是Page_Load代码:
void Page_Load(Object sender, EventArgs e) {
string strProvider=ConfigurationSettings.AppSettings["SQLConn"];
SqlConnection MyConnection=new SqlConnection(strProvider);
MyConnection.Open(); //打开联结
string strIndex1="select articleid,title,author from article where deleted=0 order by articleid desc";
SqlDataAdapter MyCommand1 = new SqlDataAdapter(strIndex1,MyConnection);
DataSet MyDataSet = new DataSet();
MyCommand1.Fill(MyDataSet);
MyDataGrid.DataSource = MyDataSet;
MyDataGrid.DataBind();
}
各位大侠指点一下!