2014-03-05 60 views
1

我正在一个项目上工作。我有一个gridview有一列有checkbox.Now我想用数据填充我的GridView并根据我的状态字段检查/取消选中相应的复选框。直到现在我正在处理gridview行数据绑定event.But,但我只想知道是否有任何方法来标记复选框,而绑定源。检查/取消选中GridView绑定源中的复选框

查询我正在执行以获得gridview的数据

select s.studentcode,s.studentname,r.status from tblstudent s join tblresults r on s.studentcode=r.studentcode 

输出我得到

enter image description here

现在我想,如果我的状态是“促进”复选框接受检查,如果是'不推广'复选框得到unchecked.I不想要使用行数据绑定方法。只想要我的工作在下面的行完成

sql = "select s.studentcode,s.studentname,r.status from tblstudent s join tblresults r on s.studentcode=r.studentcode"; 
    ds = obj.openDataset(sql, Session["SchoolCode"].ToString()); 
    if (ds.Tables[0].Rows.Count > 0) 
    { 
     grdstudents.DataSource = ds; 
     grdstudents.DataBind(); 
     grdstudents.Visible = true; 
    } 
    else 
    { 
     alertdiv.Visible = true; 
     lblalertmsg.Visible = true; 
     btnclose.Visible = true; 
     lblalertmsg.Text = "No Record found"; 
    } 
在aspx页面里面的GridView

解决方案

查询

select s.studentcode,s.studentname,if(r.status='Promoted',true,false) as status from tblstudent s left join tblresults r on s.studentcode=r.studentcode where s.classcode='10000' 

grdstudents.DataSource = ds;// note we binded to final table 
grdstudents.DataBind(); 

复选框字段:

<ItemTemplate> 
       <asp:CheckBox ID="chkapply" runat="server" Checked='<%# Convert.ToBoolean(Eval("status"))%>'/> 
      </ItemTemplate> 

该解决方案将帮助您避免额外的代码,以便在使用itembinding或行的数据绑定写入gridview事件

回答

2

只有使用布尔值才能选中/取消选中复选框。在这种情况下,因为Column:'status'不表示布尔值,所以直接绑定数据不会达到目的。

您需要按照两个步骤:

步骤1)复制原数据到新表,并更改您的“状态”一栏布尔的数据类型。

第2步。)使用原始表的数据修改最终表中状态列中的数据。

第1步:

ds = obj.openDataset(sql, Session["SchoolCode"].ToString()); 
DataTable dtOriginalTable = ds.Tables[0]; 
DataTable dtFinalTable = new DataTable(); 
foreach (DataColumn dc in dtOriginalTable.Columns) 
{ 
dtFinalTable.Columns.Add(dc.ColumnName); 
} 
foreach (DataColumn dc in dtFinalTable.Columns) 
{ 
      if (dc.ColumnName == "status") 
       dc.DataType = System.Type.GetType("System.Boolean"); 
} 

以下步骤2 ::

foreach (DataRow drow in dtOriginalTable.Rows) 
{ 
     if(drow["status"].ToString().Equals("Promoted")) 
       drow["status"]="true"; 
     else 
       drow["status"]="false" 
// ADD the row to final table 
dtFinalTable.Rows.Add(drow.ItemArray); 
} 

现在做你结合的GridView为:

grdstudents.DataSource = dtFinalTable ;// note we binded to final table 
grdstudents.DataBind(); 

注::为什么一步1是必需的?因为一旦填充了数据,就无法更改列的数据类型。

此外,当数据库有大量数据时,这将是一个性能问题。

+1

但是如何将复选框绑定到状态字段 rupinder18

+0

您应该使用“Checked”属性:'runat =“server”/>或者试试看:Checked ='<%#Convert.ToBoolean Eval(“Column_Name_Here”))%>' –

+0

ya it worked thanx – rupinder18

2

您需要为网格使用OnItemDataBoundEvent。 如果你不想使用该事件。然后你可以遍历gridview的所有行。

如果您的网格ID为grid1,则循环遍历grid1.Rows中的所有行。 您必须在行中找到您复选框,然后根据您的数据选中/取消选中。

foreach (GridViewRow row in grid1.Rows) 
{ 
    CheckBox checkBox1 = row.FindControl("checkBox1") as CheckBox; 
    checkBox1.Checked = true; // based on condition 

} 
+0

但我不想使用任何event.Is有可能直接做 – rupinder18

+0

@ rupinder18我已经更新了我的答案。 – AbhinavRanjan

相关问题