convert boolean to checkbox in Item_Bound event

H

hazz

ICollection CreateDataSource() {
dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean)));
for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr[0] = 0;
dt.Rows.Add(dr); }

void Page_Load(Object sender, EventArgs e)
{ ItemsGrid.DataSource = CreateDataSource();

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control.

????? How do I convert the boolean value to a checkbox. I don't want
to see true/false, I want to see a checkbox, checked or not.

Thank you,
-Greg
 
K

Karl Seguin [MVP]

something like:

you might have to check for e.Item.ItemType to make sure it isn't a
header/footer...


CheckBox check = (CheckBox)
e.Item.FindControl("IdOfTheRunatServerCheckbox");
check.Checked =
Convert.ToBoolean(((DataRowView)e.Item.DataItem)["ReportInclude"]);

Karl
 
H

hazz

Thank you so much Karl.

One follow up question having to do with the design time structure of the
checkbox column for the.FindControl("IdOfTheRunatServerCheckbox"); Where
does IdOfTheRunatServerCheckbox go within the <asp:> </asp?> as listed here
from my aspx page?

<asp:DataGrid id="ItemsGrid" runat="server"
OnItemDataBound="Item_Bound">
<HeaderStyle BackColor="#00aaaa"> </HeaderStyle>
<FooterStyle BackColor="#00aaaa"></FooterStyle>
</asp:DataGrid>

Thank you,
Greg

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
something like:

you might have to check for e.Item.ItemType to make sure it isn't a
header/footer...


CheckBox check = (CheckBox)
e.Item.FindControl("IdOfTheRunatServerCheckbox");
check.Checked =
Convert.ToBoolean(((DataRowView)e.Item.DataItem)["ReportInclude"]);

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


hazz said:
ICollection CreateDataSource() {
dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean)));
for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr[0] = 0;
dt.Rows.Add(dr); }

void Page_Load(Object sender, EventArgs e)
{ ItemsGrid.DataSource = CreateDataSource();

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control.

????? How do I convert the boolean value to a checkbox. I don't want
to see true/false, I want to see a checkbox, checked or not.

Thank you,
-Greg
 
K

Karl Seguin [MVP]

Sorry, I zoned out the entire datagrid thing :)

You'll need to turn off autogeneratecolumns and output it yourself:

<asp:DataGrid ID="grid" runat="Server" OnItemDataBound="Item_Bound">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="myCheckbox" runat="Server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Karl


--
http://www.openmymind.net/
http://www.fuelindustries.com/


hazz said:
Thank you so much Karl.

One follow up question having to do with the design time structure of the
checkbox column for the.FindControl("IdOfTheRunatServerCheckbox"); Where
does IdOfTheRunatServerCheckbox go within the <asp:> </asp?> as listed
here from my aspx page?

<asp:DataGrid id="ItemsGrid" runat="server"
OnItemDataBound="Item_Bound">
<HeaderStyle BackColor="#00aaaa"> </HeaderStyle>
<FooterStyle BackColor="#00aaaa"></FooterStyle>
</asp:DataGrid>

Thank you,
Greg

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
something like:

you might have to check for e.Item.ItemType to make sure it isn't a
header/footer...


CheckBox check = (CheckBox)
e.Item.FindControl("IdOfTheRunatServerCheckbox");
check.Checked =
Convert.ToBoolean(((DataRowView)e.Item.DataItem)["ReportInclude"]);

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


hazz said:
ICollection CreateDataSource() {
dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean)));
for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr[0] = 0;
dt.Rows.Add(dr); }

void Page_Load(Object sender, EventArgs e)
{ ItemsGrid.DataSource = CreateDataSource();

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control.

????? How do I convert the boolean value to a checkbox. I don't
want to see true/false, I want to see a checkbox, checked or not.

Thank you,
-Greg
 
H

hazz

When I add the following to the gridview
<Columns>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1"
Text="Taxable"
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>

CheckBox check = (CheckBox)e.Item.FindControl("CheckBox1") returns a null.


hazz said:
Thank you so much Karl.

One follow up question having to do with the design time structure of the
checkbox column for the.FindControl("IdOfTheRunatServerCheckbox"); Where
does IdOfTheRunatServerCheckbox go within the <asp:> </asp?> as listed
here from my aspx page?

<asp:DataGrid id="ItemsGrid" runat="server"
OnItemDataBound="Item_Bound">
<HeaderStyle BackColor="#00aaaa"> </HeaderStyle>
<FooterStyle BackColor="#00aaaa"></FooterStyle>
</asp:DataGrid>

Thank you,
Greg

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
something like:

you might have to check for e.Item.ItemType to make sure it isn't a
header/footer...


CheckBox check = (CheckBox)
e.Item.FindControl("IdOfTheRunatServerCheckbox");
check.Checked =
Convert.ToBoolean(((DataRowView)e.Item.DataItem)["ReportInclude"]);

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


hazz said:
ICollection CreateDataSource() {
dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean)));
for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr[0] = 0;
dt.Rows.Add(dr); }

void Page_Load(Object sender, EventArgs e)
{ ItemsGrid.DataSource = CreateDataSource();

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control.

????? How do I convert the boolean value to a checkbox. I don't
want to see true/false, I want to see a checkbox, checked or not.

Thank you,
-Greg
 
H

hazz

Thanks Karl, I'm getting closer !! I have checkable checkboxes. -Greg

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
Sorry, I zoned out the entire datagrid thing :)

You'll need to turn off autogeneratecolumns and output it yourself:

<asp:DataGrid ID="grid" runat="Server" OnItemDataBound="Item_Bound">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="myCheckbox" runat="Server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Karl


--
http://www.openmymind.net/
http://www.fuelindustries.com/


hazz said:
Thank you so much Karl.

One follow up question having to do with the design time structure of the
checkbox column for the.FindControl("IdOfTheRunatServerCheckbox"); Where
does IdOfTheRunatServerCheckbox go within the <asp:> </asp?> as listed
here from my aspx page?

<asp:DataGrid id="ItemsGrid" runat="server"
OnItemDataBound="Item_Bound">
<HeaderStyle BackColor="#00aaaa"> </HeaderStyle>
<FooterStyle BackColor="#00aaaa"></FooterStyle>
</asp:DataGrid>

Thank you,
Greg

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
something like:

you might have to check for e.Item.ItemType to make sure it isn't a
header/footer...


CheckBox check = (CheckBox)
e.Item.FindControl("IdOfTheRunatServerCheckbox");
check.Checked =
Convert.ToBoolean(((DataRowView)e.Item.DataItem)["ReportInclude"]);

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


ICollection CreateDataSource() {
dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean)));
for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr[0] = 0;
dt.Rows.Add(dr); }

void Page_Load(Object sender, EventArgs e)
{ ItemsGrid.DataSource = CreateDataSource();

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control.

????? How do I convert the boolean value to a checkbox. I don't
want to see true/false, I want to see a checkbox, checked or not.

Thank you,
-Greg
 
K

Karl Seguin [MVP]

I kinda hinted at that first...but it wasn't clear, sorry..
If (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
///do stuff
}

there won't be a "CheckBox1" when ItemType is Header or Footer or etc...
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


hazz said:
When I add the following to the gridview
<Columns>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1"
Text="Taxable"
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>

CheckBox check = (CheckBox)e.Item.FindControl("CheckBox1") returns a
null.


hazz said:
Thank you so much Karl.

One follow up question having to do with the design time structure of the
checkbox column for the.FindControl("IdOfTheRunatServerCheckbox"); Where
does IdOfTheRunatServerCheckbox go within the <asp:> </asp?> as listed
here from my aspx page?

<asp:DataGrid id="ItemsGrid" runat="server"
OnItemDataBound="Item_Bound">
<HeaderStyle BackColor="#00aaaa"> </HeaderStyle>
<FooterStyle BackColor="#00aaaa"></FooterStyle>
</asp:DataGrid>

Thank you,
Greg

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
something like:

you might have to check for e.Item.ItemType to make sure it isn't a
header/footer...


CheckBox check = (CheckBox)
e.Item.FindControl("IdOfTheRunatServerCheckbox");
check.Checked =
Convert.ToBoolean(((DataRowView)e.Item.DataItem)["ReportInclude"]);

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


ICollection CreateDataSource() {
dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean)));
for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr[0] = 0;
dt.Rows.Add(dr); }

void Page_Load(Object sender, EventArgs e)
{ ItemsGrid.DataSource = CreateDataSource();

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control.

????? How do I convert the boolean value to a checkbox. I don't
want to see true/false, I want to see a checkbox, checked or not.

Thank you,
-Greg
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top