1

Hi I want to bind my gridview from a datatable which I create myself.

It's works but my columns are duplicated, I don't understand exactly why.

Here is my gridview :

<asp:GridView runat="server" ID="GvCalculSelect" CssClass="gridView" HorizontalAlign="Center">
                <Columns>
                    <asp:BoundField DataField="CalculName" HeaderText="CalculName" />
                    <asp:TemplateField HeaderText="ResultValue">
                        <ItemTemplate>
                            <asp:CheckBox ID="CbResultValue" runat="server" Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "ResultValue").ToString()) %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

And here it's a part of my cs code :

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindGrid();
    }
}

 protected void BindGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("CalculName", typeof(string)));
    dt.Columns.Add(new DataColumn("ResultValue", typeof(Boolean)));
    dt.Rows.Add("Test", true);
    GvCalculSelect.DataSource = dt;
    GvCalculSelect.DataBind();
}

The result is :
enter image description here

How to fix it ? I think when I bind my gridview is taking on board my two "dt.columns.add". I just want to keep my calculName field and my resultValue checkbox

Thanks for your help

1
  • make sure your auto generate columns is set to false. also on the Bind() method add this as your first line GvCalculSelect.DataSource = null; Commented Mar 21, 2016 at 15:17

2 Answers 2

2

This is happening because you are creating your own table and binding it to the GridView AS WELL AS defining 2 columns explicitly in the Gridview and binding datafields to them.

Do 1 or the other, but not both. So, to be clear if you want to control the look and feel of your columns you should set the autogeneratecolumns property to false. Keep your front end bound columns, but you can then remove them from the datatable and simply reference the relevant fields that are present in the datatable.

Alternatively, set autogeneratecolumns to true, but then construct the columns in your datatable in your code behind and then just bind it as is.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes that's I am thinking but I have no idea to insert properly the data directly into my gridview without pass by a datatable which I create myself
@Ironboy07 - sounds like you want some control over what the columns looks like etc. So, you don't need to create new columns in your datatable. Set autogeneratecolumns to false and just fill the datatable, bind it and then use the boundfield and templatefield to reference the correct data.
Yes thanks it works. I just set the autogeneratecolumns to false like you said. I forgot this parameters... Thanks for your quick answer !
@Ironboy07 - np. Please mark this as the answer if it worked for you.
1

This is because GridView.AutoGenerateColumns Property

You need to set its value false.

For your refrence according to MSDN

If you set this property to true and set the ItemType property to a model type, DynamicField controls are generated.

If you do not set the ItemType property, BoundField controls are generated. If you do not want DynamicField controls, you have the following options:

  1. Set the ColumnsGenerator property to null in the Page_Load event handler. In that case, BoundField controls are generated.

  2. Write custom code to automatically generate fields by creating and assigning your own ColumnsGenerator class and assigning an instance of it to the control.

  3. Set AutoGenerateColumns to false. In that case, no fields are generated, and you must manually specify fields using controls such as BoundField or ImageField.

  4. Do not set the ItemType property. In that case, BoundField controls are generated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.