Datagrid Problem

kekinlim

Member
Joined
Jan 12, 2005
Messages
10
Programming Experience
Beginner
hihi,

I am creating a datagrid and i wanted to display all the values that i retrieve from the database. I was able to do this. However, i need to add a checkbox into one of the columns. This checkbox is not binded or link to the database, it is use for my own reference. I was wondering how can i create this checkbox into the datagrid without any mappingname. Hope someone can help me with this. Thank You..

Kekin
vb.net Programmer
 
I don't beleive the dataGrid can combine bound and unbound columns.
What you'll need to do is add a column to the underlying dataSource and create a TableStyle that includes a reference to that column.
Here's some sample code showing how to add a boolean column to a dataSet table:
VB.NET:
Dim col As DataColumn = New DataColumn("MyRef")
col.DataType = GetType(Boolean)
col.DefaultValue = False
objdsDataForm.Tables(0).Columns.Add(col)
where objdsDataForm is a dataSet.
 
Hi Paszt,

Thanks for your help. I can display another column in the datagrid using ur sample code. But I don't really understand what you mean by "create a TableStyle that includes a reference to that column."


Kekin
VB.Net Programmer
 
Hi Paszt,

Think i got it, the checkbox is actually there... But how can i move its postion. ie. to the first column of the table.. Thanks..

Kekin
VB.Net Programmer
 
hi Kekin,

If you have are been able to display checkbox in your datagrid then its relatively easier to display as the first column of your datagrid. You can do this in your datagrid declaration where you actually design your columns with their names respectively.

All you need to do is to move your column for checkboxes into the first place. This is a code snippet for the declaration of datagrid in vb.net. I studied about this from 4guysfromrolla.com website, although i could not find the article related to it that is why i am giving you this code. You can figure it out whats in it and if you have questions do let me know.

Note: I have a invisible column of userID. I need it because that will help me identify individual record by its userID but my Datagrid does not display it when page is loaded.

hope this helps,
Vikram

--------------------------------------------------------------------------
<asp:datagrid ID="MyDataGrid" runat="server"
AutoGenerateColumns="False" EnableViewState="True"
AlternatingItemStyle-BackColor="#ffffcc"
FooterStyle-BackColor="#6699ff"
HeaderStyle-BackColor="#6699ff"
Font-Size="8pt" Font-Name="Arial" CellSpacing="0" CellPadding="3"
ShowFooter="false" BorderColor="black" BackColor="#ffffff" Width="97%"
PagerStyle-Mode="NumericPages"
PageSize="300" AllowPaging="False"
AllowSorting="true" OnSortCommand="MyDataGrid_Sort">
<columns>
<asp:templatecolumn HeaderText="Notify" ItemStyle HorizontalAlign="Center">
<itemtemplate>
<asp:Checkbox Id="cb" runat="server" />
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn HeaderText="User ID" DataField="UserID" SortExpression="UserID" Visible="false"/>
<asp:TemplateColumn HeaderText="User ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUserID" Text='<%# DataBinder.Eval(Container.DataItem,"UserID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Organization" SortExpression="Company">
<ItemTemplate>
<asp:Label ID="lblUserOrganization" Text='<%# DataBinder.Eval(Container.DataItem,"Company") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Contact Name" SortExpression="ContactName">
<ItemTemplate>
<asp:Label ID="lblUserContactName" Text='<%# DataBinder.Eval(Container.DataItem,"ContactName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn HeaderText="Title" DataField="ContactTitle"/>
<asp:TemplateColumn HeaderText="Contact Email">
<ItemTemplate>
<asp:Label ID="lblContactEmail" Text='<%# DataBinder.Eval(Container.DataItem,"ContactEmail") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn HeaderText="Phone" DataField="ContactPhone" HeaderStyle-HorizontalAlign="center" />
<asp:boundcolumn HeaderText="Expiration Date" DataField="ExpDate" DataFormatString="{0:d}"
SortExpression="ExpDate"/>
<asp:templatecolumn HeaderText="Details/Edit">
<itemtemplate>
<center>
<a href="../User_Modify.aspx?MenuItem=<%# Request.QueryString("MenuItem") %>&UserID=<%# DataBinder.Eval(Container.DataItem, "UserID")%>&Level=2">Details</a>
</center>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
--------------------------------------------------------------------------
 
I was assuming Winforms.
If that's the case and you created the TableStyle with the designer, all you need to do is use the Up and Down arrows in the 'DataGridColumnStyle Collection Editor' to move the location of the columns.
 
Back
Top