Click to See Complete Forum and Search --> : Binding Data to Template columns in a DataGrid


ShadowGopher
April 30th, 2003, 12:24 PM
I've got a datagrid that is bound to a typed dataset. When I do a DataBind() method, everything works fine until I change some of the columns to template columns.

I have 2 columns that are SQLServer bit columns that I want to display as checkboxes. So, I changed the columns to template columns, edited the template columns and changed the ItemTemplate to a checkbox, went to the top of the property box and chose Databinding, and then from the list I set the checked property to be bound to the dataset's datacolumn for that column. The syntax in the html code of the aspx page is as follows:
<asp:TemplateColumn HeaderText="Load">
<HeaderStyle HorizontalAlign="Center" Width="35px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<asp:Label id="Label3" runat="server" Font-Bold="True">Load</asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id=cbxLoadFlag runat="server" Checked='<%# DataBinder.Eval(filesDS, "Tables[DataFile].DefaultView.[0].load_flag") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>


The problem is that while the other columns are binding correctly, it appears that the syntax inserted for the DataBinder.Eval method is pulling the values from the 1st row of the dataset, not the correct row.

Does anyone know how I can bind these template columns so that the correct dataset row is bound to the corresponding datagrid row?

Thanks in advance,
Mark Brown

ShadowGopher
May 19th, 2003, 02:33 PM
Well, I finally found the answer. I was using the wrong method of binding to the datagrid. The correct way follows:
<asp:TemplateColumn HeaderText="Load">
<HeaderStyle HorizontalAlign="Center" Width="35px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<asp:Label id="Label3" runat="server" Font-Bold="True">Load</asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id=cbxLoadFlag runat="server" Checked='<%# DataBinder.Eval(Container, "DataItem.load_flag") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>