p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ADO.NET (http://p2p.wrox.com/forumdisplay.php?f=109)
-   -   How to display checkbox in a DataGrid (http://p2p.wrox.com/showthread.php?t=17202)

kmazumdar August 20th, 2004 03:36 PM

How to display checkbox in a DataGrid
 
Hi,

Another beginer's question :

I am still working on my very first form using ASp.NET and C# - so please bear with me.

I am fetching results into a DataGrid. One of the column has a Y or N value and I want show a Checkbox ( Checked if 'Y' and non-checked, if 'N'). I also need to change background color of the row, if the value is 'N'. Here is my ItemTemplate :
Code:

<asp:BoundColumn DataField="ACTIVE_IND" ItemStyle-HorizontalAlign="Center" HeaderText="Active Indicator"></asp:BoundColumn>
Data for this DataGrid is bound in the click event of a submit button.

Code:

DataGrid1.DataSource = ds.Tables["nnld"];
DataGrid1.DataBind();

Thanks in advance.



mehdi62b August 23rd, 2004 09:47 AM

its not a beginer's question.!
I think you should have a checkbox somewhere like ItemTemplate and also you
fill your grid with your datasource,well
for getting what is bound to your filed you should work with
yourDataGridItem.DataItem.
and also for changing the backcolor,you can work with yourDataGridItem.CssClass
and also you should have a <STYLE></STYLE> tag in your aspx file and set the CssClass
with one of the styles declared in your <STYLE> tag.
but where you can work with them?surely somewhere like DataGrid1_ItemDataBound
have a look at this example...
Code:

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem ||
    e.Item.ItemType == ListItemType.SelectedItem ||
    e.Item.ItemType == ListItemType.EditItem)
        {   
            if(((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()=="aSpecialValue")
            {
                ((CheckBox)e.Item.Cells[0].FindControl("yourUniqueCheckBoxID")).Checked=true;
                e.Item.CssClass="YourNewCssDecalredInStyleTag";
            }
        }
}

this example marks every item that aSpecialValue has been bound to it also change it's
CSS changes according to YourNewCssDecalredInStyleTag.
HtH.

--------------------------------------------
Mehdi.:)

kmazumdar August 23rd, 2004 03:35 PM

Thank you. Let me try your suggestion.

Thanks.



All times are GMT -4. The time now is 04:10 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.