how to check/Uncheck a checkbox dynamically in a datagrid in asp.net

N

Neal

HI all,

I have an issue with check box in datagrid.
I would like it to check and uncheck based on a database value of NULL
or a Date Value.

If there is a date value for that column then check the check box in
datagrid

If the value is null then keep it unchanged as (Unchecked)

this is in asp.net

Your help is appreciated

Thank you

Neal
 
S

surindersaini

hi,

Datagrid code with check box and date field

<asp:DataGrid id="grdAuthors" runat="server" Width="626px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkbox" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>

and with dataItemBound event of grid u can write code as below


string _lblDate = e.Item.Cells[1].Text.ToString(); //date column
CheckBox _chkBox = (CheckBox)e.Item.FindControl("chkbox");
if(_chkBox != null)
{
if(_lblDate == "True")//Here u can check if some date text is
//bound with the label
{
_chkBox.Checked = true;
}
}
 
Joined
Mar 1, 2012
Messages
1
Reaction score
0

Hi,

Try this... it vl help u.
i used gridview.
got the id of the student.
as the userid is in a datalist. so i too from ther.
check the active status from a table.
i used checkboxes. if the user is in an active status then boxes should be checked.
protected void dgGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Separator)
{
Label lblstudentname = (Label)e.Row.FindControl("lblstudentname");
string studentname = lblstudentname.Text.Trim();
Label lbluserid = (Label)e.Row.FindControl("lbluserid");
string userid = lbluserid.Text.Trim();
DataSet ds1 = comProfile.getAssessmentChar();
DataList dldata = (DataList)e.Row.FindControl("dldata");
for (int i = 0; i < dldata.Items.Count; i++)
{
Label lblid1 = (Label)dldata.Items.FindControl("lblid1");
string lsid = lblid1.Text.Trim();
int lsid1 = Convert.ToInt32(lsid);
CheckBox chkactive = (CheckBox)dldata.Items.FindControl("chk1");
DataSet ds = comProfile.getDetails(userid, iyear, lsid);

if(ds.Tables[0].Rows.Count > 0)
chkactive.Checked = true;
else
chkactive.Checked = false;
}
}
}

Regards
suba


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top