0

Hi,

I have bound the data in the grid view using the following code

gvHolidaysLst.DataSource = dsHoliday;
       gvHolidaysLst.DataBind();

But I wanted to add a new column with check box control in the grid view.An error occurs while filling the grid view because the data source contains only 3 columns but i have added checkbox control as the fourth column.
How to resolve this issue?

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by cocoll
0

hello!
your post is not clear.
if you have a data source that return 3 columns and you have added a column to the gridview so it will not give any error,so post your aspx code,meanwhile i assume that you have try this code for the forth column:

<asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:CheckBoxField />
        </Columns>
    </asp:GridView>

and with your code behind it will work perfectly.
Regards!

0
<asp:GridView ID="gvHolidaysLst" runat="server" AutoGenerateColumns="False" 
    BorderStyle="None" Caption="List of Holidays" CellPadding="4" 
    ForeColor="#333333" Height="127px" Width="391px" >
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
        <asp:BoundField DataField="DATE" HeaderText="Date" />
        <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" />
        <asp:BoundField DataField="HALFFULL" HeaderText="Half/Full" />
        <asp:CheckBoxField DataField=" " Text="Delete" />
    </Columns>
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

This is the code that I have used.

0

hello again.
first you have to add a field into the table in the database of type "bit" that can support 2 values: true or false.
so if i suppose that this field name is "cb" of type "bit" in the table in your database,so first thing you have to retrieve it with 'DATE','DESCRIPTION','HALFFULL' columns.
second in your aspx file,replace this row:

<asp:CheckBoxField DataField=" " Text="Delete" />

by this:

<asp:TemplateField HeaderText="test">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("cb") %>'/>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("cb") %>' 
                        Enabled="true" AutoPostBack="true" OnCheckedChanged="Checkbox1_Checked"/>
                </ItemTemplate>
            </asp:TemplateField>

this will allow you to use this checkbox field in your gridview,so it will be editable.
and for code behind:

protected void Checkbox1_Checked(object sender, EventArgs e)
        {
            //whatever you want.
        }

but if you want that the checkbox is readOnly,so don't do anything from this,so simply:

<Columns>
        <asp:BoundField DataField="DATE" HeaderText="Date" />
        <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" />
        <asp:BoundField DataField="HALFFULL" HeaderText="Half/Full" />
        <asp:CheckBoxField [B]DataField="cb"[/B] Text="Delete" />
    </Columns>

Regards!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.