Gridview bound to Object and Checkbox

R

r.hein

I've got a middle tier data object that I'm binding a gridview to, and
I'm having some problems with the check box, and having it enabled.

Let'd start with the middle tier object (and the corresponding
collection). The object is simple - it's got some properties (only one
has a setter), and a constructor.
public class Info
{
private int _ID;
private DateTime _createDate;
private int _Age;
private bool _selected;

public int ID
{
get
{
return ID;
}
}
public DateTime CreateDate
{
get
{
return _createDate;
}
}
public int Age
{
get
{
return _Age;
}
}
public bool Selected
{
get
{
return _selected;
}
set
{
_selected = value;
}
}
public Info( int ID, DateTime CreateDate, int Age)
{
_ID = ID;
_createDate = CreateDate;
_Age = Age;
_selected = false;
}

}

There's also an object InfoList that inherits from the generic list
List<Info>, basically what happens is that in it's constuctor it walks
a dataset from another object and populates itself with Info objects

public class InfoList:List<Info>
{

public InfoList():this((true))
{
}
public InfoList(bool LoadDefaultData)
{

if (LoadDefaultData)
{


InfoBase baseDS = new InfoBase();
DataTable tempTable = baseDS.Tables[0];
foreach (DataRow tempRow in tempTable.Rows)
{
Info tempInfo = new
Info(Convert.ToInt32(tempRow["ID"])
,
Convert.ToDateTime(tempRow["CreateDate"])
,
Convert.ToInt32(tempRow["Age"]));

this.Add(tempInfo);
}
}
}
public List<Info> SortList(int maximumRows, int startRowIndex,
string SortExpression){
// Code left out for brevity, this is the Select method used
by the gridview
}
public int GetCount(){
return this.Count;
}
}

Here's the Gridview code

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
DataSourceID="UnsoldInventory" Width="725px" CellPadding="0"
BorderWidth="0px"
AllowSorting="True" PageSize="10"
EnableSortingAndPagingCallbacks="True"
EnableViewState="False" HorizontalAlign="Left" ShowFooter="True">
<Columns>
<asp:CheckBoxField DataField="Selected" Text="Buy Now!" >
<ItemStyle CssClass="bt" HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:BoundField DataField="ID" Visible="false">
<ItemStyle CssClass="bt" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="CreateDate" HeaderText="Create Date"
SortExpression="CreateDate" ReadOnly="True" >
<ItemStyle CssClass="bt" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Age" HeaderText="Create Date"
SortExpression="Age" ReadOnly="True" >
<ItemStyle CssClass="bt" HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="White" />
<HeaderStyle CssClass="ftt" HorizontalAlign="Center" />
<SelectedRowStyle ForeColor="Red" Font-Bold="False"
BackColor="#BBBBBB"></SelectedRowStyle>
<AlternatingRowStyle BackColor="#EDEDED" />
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<PagerStyle ForeColor="Black" HorizontalAlign="Right"
BackColor="#C6C3C5" />
</asp:GridView>
<%--<i>You are viewing page
<%=GridView1.PageIndex + 1%>
of
<%=GridView1.PageCount%>
</i>--%>
<asp:ObjectDataSource ID="UnsoldInventory" runat="server"
SelectMethod="SortList"
TypeName="InfoList" EnablePaging="true"
SortParameterName="SortExpression" SelectCountMethod="getCount" >
</asp:ObjectDataSource>

So my question is this: I need the check boxes tied to the Selected
property of the Info object to be able to be checked and unchecked, so
I can know which items the end user wants. I don't want to have to have
the user edit one row at a time, and I've got
EnableSortingAndPagingCallbacks="True" on which prevents me from using
a template - I get an error message about Templates not being supported
- If I turn off the EnableSortingAndPagingCallbacks then the paging and
sorting quits working.

I've been unable to find an example for doing this, I find it hard to
beleive that no one at MS thought people would want this functionality.
BTW this is .NET 2.0 if that matters.

Any ideas?

Thanks.
 
P

Pao

So my question is this: I need the check boxes tied to the Selected
property of the Info object to be able to be checked and unchecked, so
I can know which items the end user wants.
I've been unable to find an example for doing this, I find it hard to
beleive that no one at MS thought people would want this functionality.
BTW this is .NET 2.0 if that matters.

Any ideas?

Thanks.

Well I have the same problem; I tried the Itemtemplate but throws a
invalid cast exception, then I tried to change the property of my
object from bool to int, but nothing to do.

Really anyone knows how to get around?
 
P

Pao

So my question is this: I need the check boxes tied to the Selected
property of the Info object to be able to be checked and unchecked, so
I can know which items the end user wants. I don't want to have to have
the user edit one row at a time, and I've got
EnableSortingAndPagingCallbacks="True" on which prevents me from using
a template - I get an error message about Templates not being supported
- If I turn off the EnableSortingAndPagingCallbacks then the paging and
sorting quits working.

I've been unable to find an example for doing this, I find it hard to
beleive that no one at MS thought people would want this functionality.
BTW this is .NET 2.0 if that matters.

Any ideas?

Thanks.


Same problem.
Really anyone knows how to solve? documentation is really poor...
I get the error that the value of the checkbox
"could not be parsed as a boolean"
but I tried also to change from Bool to Int the property of my business
object.
 
P

Pao

Pao said:
Same problem.
Really anyone knows how to solve? documentation is really poor...
I get the error that the value of the checkbox
"could not be parsed as a boolean"
but I tried also to change from Bool to Int the property of my business
object.


I found the solution
As I operate with MySql, simply putting the boolean field as
ENUM('True','False') default 'False', permit the <asp:CheckBoxField to
bind correctly.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top