CheckBox control in a datagrid

  • Thread starter Thread starter Jennyfer J Barco
  • Start date Start date
J

Jennyfer J Barco

Hello, I have a datagrid that brings some information from a query. I need
to have a checkbox in each row so the user can select the rows he wants to
reprint. Is it possible to have a checkbox control in a datagrid? Now I
have a Select column but the problem is that the user needs to select more
than one record and send them all to print, instead of being one by one like
is now with the Select column.

Another question, is it possible to have a row with diferent color in a
grid? For example I have a column called Inv. that brings the inventory
stock and everytime they have 0 in this field they want to see the row in
color red so they can identify they're out of ctock for that item, and the
items that have more than 0 still show in color black. Is this possible?

Thanks in advance
jennyfer
 
Jennyfer:
Take a look at: http://openmymind.net/databinding/index.html it should
atleast answer your second question. Basically you can use the
OnItemDataBound even, get the data being bound and if it's something (like a
0) give you the control to change UI elements (like a red background).

As for your first question. You can use a CheckBox control in each row, and
add a 'Print' button at the bottom (in the footer template). When the
button is clicked, you loop through each row of the grid (for each row as
DataGridItem in Grid.Items) find the checkbox:

dim chk as Checkbox = (CheckBox)row.FindControls("checkBoxId")
if not chk is nothing andalso chk.Checked = true then
'this item is checked, maybe add them all to arraylists, then loop
through those and print each corresponding record
end if


the tutorial should help you with that as well :)

Karl
 
Thanks so much, the first part worked perfectly, but creating a checkbox in
a datagrid is the problem. In the properties of the grid I can't find a way
to define the column as a checkbox type.

Thanks so much
 
You can create a TemplateColumn and add any type of control you need,
it would look something like:

<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox runat="server" ... />
</ItemTemplate>
</asp:TemplateColumn>

I have an article with some tips on using controls from inside a grid
template column (it uses a DropDownList, but the workings are all the
same):

http://odetocode.com/Articles/231.aspx

HTH!
 
Thanks it worked.

Now I have a question, if a need to bind a checkbox control to a field that
has a value "Y" or "N", if the data is "Y" I need to appear checked, if "N"
I need it to appear unchecked. How can I do this? where do I bind the
control?

Now in the grid I have
<asp:BoundColumn DataField="lcalculatedlastfreeday"
SortExpression="lcalculatedlastfreeday" HeaderText="Calc last free
day"></asp:BoundColumn> and it's appearing the "N" or "Y". I need to appear
a checkbox instead of the "Y", "N" value.

Thanks in advance
jennyfer
 
Back
Top