locked
Dynamic gridview column with boundfield RRS feed

  • Question

  • User-866309594 posted

    hi,

    i have nested grid view, with collapse and expand child grid view 
    the problem is that the data source of this Parent grid view is dynamic, which mean that the header name and number of columns are changing depend of the entered value, and for parent gridview i need to make the AutoGenerateColumns to false and specify the column using BoundField because i need to use  TemplateField, but i will get an error

    A field or property with the name 'service' was not found on the selected data source


    because coulmn name [service] is changing

    my code

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AllowPaging="True"
     ShowFooter="true" DataKeyNames="userid" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="gridView_PageIndexChanging">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <a href="javascript:expandcollapse('div<%# Eval("userid") %>', 'one');">
                                        <img id="imgdiv<%# Eval("userid") %>" alt="Click to show/hide <%# Eval("userid") %>"
                                            width="9px" src="../images/plus.gif" />
                                    </a>
                                </ItemTemplate>
                            </asp:TemplateField>
                          	<asp:BoundField DataField="engname" HeaderText="Provider" ReadOnly="True" SortExpression="engname" />
                          	<asp:BoundField DataField="service" ReadOnly="True"  SortExpression="service" ItemStyle-HorizontalAlign="Center" />                                          	<asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" SortExpression="Total" ItemStyle-HorizontalAlign="Center" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <tr>
                                        <td>
                                            <div id="div<%# Eval("userid") %>" style="display: none; ; left: 15px;
                                                overflow: auto">
                                                <asp:GridView ID="GridView2"  AllowSorting="true" AutoGenerateColumns="false" 
                                                    runat="server" DataKeyNames="CurrentActorUID" ShowFooter="true" 
                                                    OnPageIndexChanging="GridView2_PageIndexChanging">
                                                     <Columns>
                                                        <asp:BoundField DataField="ReferenceNo" HeaderText="ReferenceNo" ReadOnly="True" />
                                                        <asp:BoundField DataField="Initiator" HeaderText="Requester" ReadOnly="True" ControlStyle-Width="50px" />
                                                        <asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True" />                                 
                                                    </Columns>
                                                </asp:GridView>
                                            </div>
                                        </td>
                                    </tr>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

    and the sql query in the stored procedure 

    select count(*) as Total,EngName, CurrentActorUID,ReferenceNo, Status,RequestedDate, ServiceNo,ServiceName,ServiceNo + ':' + ServiceName as [service]
    from tbl_ServiceTracking

    i found that i need to create a data table and try to insert the required column to it so i modifyied the code as follow

    DataSet myDataSet = new DataSet();
                    mySqlAdapter1.Fill(myDataSet);
                    DataTable dt = new DataTable();
                    mySqlAdapter1.Fill(dt);
                    if (dt != null)
                    {
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Columns.Count; i++)
                            {
                                BoundField boundField = new BoundField();
                                if (dt.Columns[i].ColumnName.ToString() != "userid")
                                {
                                    boundField.DataField = dt.Columns[i].ColumnName.ToString();
                                    boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
                                    GridView1.Columns.Add(boundField);
                                 
                                }
    
                            }
                        }
                    }
                    GridView1.DataSource = dt;
                   GridView1.DataBind();

    but still i am getting duplicate column

    can anyone help me



    Monday, July 23, 2012 7:02 AM

Answers

  • User-1635004338 posted

    Hi,

    Move the code of building columns and binding GridView to the Page_Init event.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2012 3:17 AM

All replies

  • User165519407 posted

    From your code, remove bounf fields from HTML designer

    the duplicates will be removed

    Monday, July 23, 2012 7:29 AM
  • User-451195045 posted
    AutoGenerateColumns="true" 

    here might be the problem

    AutoGenerateColumns="false" 

    Hope this will help.
    <script id="__changoScript" type="text/javascript">// <![CDATA[ var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = true;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})(); // ]]></script>
    Monday, July 23, 2012 7:34 AM
  • User-866309594 posted

    thank you for the replies

    i modified my code and its working but the grid view is not arranged because in parent gridview i am using

    <asp:TemplateField>
    <ItemTemplate>
    <a href="javascript:expandcollapse('div<%# Eval("userid") %>', 'one');">
    <img id="imgdiv<%# Eval("userid") %>" alt="Click to show/hide <%# Eval("userid") %>"
    width="9px" src="../images/plus.gif" />
    </a>
    </ItemTemplate>
    </asp:TemplateField>
    

    and the source code

    DataTable dt = new DataTable();
    mySqlAdapter1.Fill(dt);
    if (dt != null)
    {
    if (dt.Rows.Count > 0)
    {
    for (int i = 0; i < dt.Columns.Count; i++)
    {
    BoundField boundField = new BoundField();
    
    boundField.DataField = dt.Columns[i].ColumnName.ToString();
    boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
    
    GridView1.Columns.Add(boundField);
    }
    }
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();

    note that in the first gridview the + sign in shown in separate row and its should be in the same row see the image 

    any help

    Tuesday, July 24, 2012 2:48 AM
  • User-1635004338 posted

    Hi,

    Move the code of building columns and binding GridView to the Page_Init event.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2012 3:17 AM
  • User-866309594 posted

    hi i found very code site that enable me to create ItemTemplate from server side in code, and its working perfectly, the problem is now how write this aspx code daynamicly from server side

    <a href="javascript:expandcollapse('div<%# Eval("userid") %>', 'one');">
        <img id="imgdiv<%# Eval("userid") %>" alt="Click to show/hide <%# Eval("userid") %>"
                                            width="9px" src="../images/plus.gif" />
    </a>

    can any one help me

    Thursday, July 26, 2012 2:07 AM
  • User-1635004338 posted

    Hi,

    The code is using client side control. You cannot writeit dynamically from server side.

    Thanks,

    Thursday, July 26, 2012 2:39 AM