title: ASP.NET - Dynamic grid with more than one DataSource
body: <p>When I am almost done with my application, one final thing is disturbing me.</p>
  
  <p>When I select an Item from DropDown list, GridView1's columns are showing up two times like this:</p>
  
  <p><img src="https://farm4.static.flickr.com/3521/3854689595_a0b860f157.jpg" alt="alt text"></p>
  
  <p>This is my aspx file:</p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebmasterControlPanel.aspx.cs" Inherits="Ice_Web_Portal.UI.Webmaster.WebmasterControlPanel" Title="Untitled Page" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt;
      &lt;table style="width: 288px"&gt;
          &lt;tr&gt;
              &lt;td valign="top"&gt;
                  &lt;asp:DropDownList ID="tableNamesDropDownList1" runat="server" DataSourceID="UserTablesObjectDataSource1"
                      DataTextField="TableName" DataValueField="TableName" Width="256px" OnSelectedIndexChanged="tableNamesDropDownList1_SelectedIndexChanged" AutoPostBack="True"&gt;
                  &lt;/asp:DropDownList&gt;&lt;br /&gt;
                  &lt;asp:ObjectDataSource ID="UserTablesObjectDataSource1" runat="server"
                      SelectMethod="GetAllUserTableNames" TypeName="Ice_Web_Portal.BO.DBTables"&gt;&lt;/asp:ObjectDataSource&gt;
              &lt;/td&gt;
              &lt;td style="width: 3px"&gt;
                  &amp;nbsp;&lt;/td&gt;
              &lt;td&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;asp:GridView ID="GridView1" runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"&gt;
                  &lt;/asp:GridView&gt;
              &lt;/td&gt;
              &lt;td style="width: 3px"&gt;
              &lt;/td&gt;
              &lt;td&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
              &lt;/td&gt;
              &lt;td style="width: 3px"&gt;
              &lt;/td&gt;
              &lt;td&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>This is my codebehind:</p>
  
  <pre><code>using System;
  using System.Data;
  using System.Configuration;
  using System.Collections;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  
  
  using System.Web.UI.HtmlControls;
  
  using Ice_Web_Portal.BO;
  using Ice_Web_Portal.ASP.NET.Utils;
  
  namespace Ice_Web_Portal.UI.Webmaster
  {
      public partial class WebmasterControlPanel : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
  
          protected void tableNamesDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
          {
              CreateGridView();
          }
  
          protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
          {
              int index = Convert.ToInt32(e.CommandArgument);
  
              if (e.CommandName == "Edit")
              {
                  GridView1.EditIndex = index;
  
                  GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
  
                  string id = selectedRow.Cells[1].Text;
                  string isbn = selectedRow.Cells[2].Text;
  
                  CreateGridView();
              }
              else if (e.CommandName == "Update")
              {                
                  LinkButton updateButton = (LinkButton)e.CommandSource;
  
                  DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
  
                  GridViewRow gvr = (GridViewRow)dcfc.Parent;
  
                  ControlCollection cc = gvr.Cells[1].Controls;
  
                  TextBox tb = (TextBox)cc[0];
  
                  GridView1.EditIndex = -1;
  
                  CreateGridView();
              }
          }
  
          private void UpdateTableData()
          {
          }
  
          private void CreateGridView()
          {
              GridView1.Columns.Clear();
  
              string tableName = tableNamesDropDownList1.SelectedItem.Text;
  
              TableNameEnum tableType = TableNameEnumUtility.ConvertToEnum(tableName);
  
              DataTable dataTable = null;
  
              if (tableType == TableNameEnum.Book)
              {
                  dataTable = Book.GetBooksDataSet().Tables[0];
              }
              else if (tableType == TableNameEnum.Course)
              {
                  dataTable = Course.GetCoursesDataSet().Tables[0];
              }
              else
              {
                  dataTable = null;
              }
  
              //Adding Edit Button
              CommandField cf = new CommandField();
              cf.ShowEditButton = true;
              GridView1.Columns.Add(cf);
  
              if (dataTable != null)
              {
                  foreach (DataColumn c in dataTable.Columns)
                  {
                      BoundField boundField = new BoundField();
  
                      boundField.DataField = c.ColumnName;
                      boundField.HeaderText = c.ColumnName;
  
                      GridView1.Columns.Add(boundField);
                  }
              }
  
              GridView1.DataSource = dataTable;
              GridView1.DataBind();
          }
  
          protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
          {
          }
  
          protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
          {
          }
  
          protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {
          }
      }
  }
  </code></pre>