Columns are displayed twice

Discussion in 'ASP .Net Datagrid Control' started by Phani, Apr 26, 2005.

  1. Phani

    Phani Guest

    I have two datagrids .... after handling the click event in one datagrid
    ..... using its data i query the database and get the data for the second
    datagrid

    in both the grids i am generating the columns dynamically

    the problem is in the second datagrid the columns are appearing twice

    i dont know y ??

    like if "a |b |c|" are the actual columns to be displayed

    they r displayed as

    a|b|c|a|b|c|



    can anyone help me???/

    this is my onitemcommand code

    protected void Item_click(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {

    int key = Convert.ToInt32 (DataGrid1.DataKeys[e.Item.ItemIndex]);

    SqlDataReader read = project.GetSingleProject (key);

    if(read.Read ())

    {

    string name = read["ProjectName"].ToString ();

    Label1.Text = "Tasks Related to "+name;

    }

    TasksDB task=new TasksDB ();

    DataSet ds = task.GetTasks (key);

    DataGrid2.DataSource =ds;

    DataGrid2.AutoGenerateColumns =false;

    foreach(DataColumn c in ds.Tables [0].Columns )

    DataGrid2.Columns .Add (CreateBoundColumn(c));

    DataGrid2.DataBind ();

    }
     
    Phani, Apr 26, 2005
    #1
    1. Advertising

  2. Phani

    Elton W Guest

    Hi Phani,

    It seems you set the datagrid's AutoGenerateColumns =
    True. And you also create some columns manually. If so,
    just set AutoGenerateColumns = false.

    HTH

    Elton Wang



    >-----Original Message-----
    >I have two datagrids .... after handling the click event

    in one datagrid
    >..... using its data i query the database and get the

    data for the second
    >datagrid
    >
    >in both the grids i am generating the columns dynamically
    >
    >the problem is in the second datagrid the columns are

    appearing twice
    >
    >i dont know y ??
    >
    >like if "a |b |c|" are the actual columns to be

    displayed
    >
    >they r displayed as
    >
    >a|b|c|a|b|c|
    >
    >
    >
    >can anyone help me???/
    >
    >this is my onitemcommand code
    >
    >protected void Item_click(object source,
    >System.Web.UI.WebControls.DataGridCommandEventArgs e)
    >
    >{
    >
    >int key = Convert.ToInt32 (DataGrid1.DataKeys

    [e.Item.ItemIndex]);
    >
    >SqlDataReader read = project.GetSingleProject (key);
    >
    >if(read.Read ())
    >
    >{
    >
    >string name = read["ProjectName"].ToString ();
    >
    >Label1.Text = "Tasks Related to "+name;
    >
    >}
    >
    >TasksDB task=new TasksDB ();
    >
    >DataSet ds = task.GetTasks (key);
    >
    >DataGrid2.DataSource =ds;
    >
    >DataGrid2.AutoGenerateColumns =false;
    >
    >foreach(DataColumn c in ds.Tables [0].Columns )
    >
    >DataGrid2.Columns .Add (CreateBoundColumn(c));
    >
    >DataGrid2.DataBind ();
    >
    >}
    >
    >
    >.
    >
     
    Elton W, Apr 26, 2005
    #2
    1. Advertising

  3. Phani

    Phani Guest

    hi Elton

    i have set that off but the same problem persists ....
    anyway thanks for ur reply .... and do reply me if u find any solution to
    this

    regards
    Phani

    "Elton W" <> wrote in message
    news:000501c54a88$9938a080$...
    > Hi Phani,
    >
    > It seems you set the datagrid's AutoGenerateColumns =
    > True. And you also create some columns manually. If so,
    > just set AutoGenerateColumns = false.
    >
    > HTH
    >
    > Elton Wang
    >
    >
    >
    >>-----Original Message-----
    >>I have two datagrids .... after handling the click event

    > in one datagrid
    >>..... using its data i query the database and get the

    > data for the second
    >>datagrid
    >>
    >>in both the grids i am generating the columns dynamically
    >>
    >>the problem is in the second datagrid the columns are

    > appearing twice
    >>
    >>i dont know y ??
    >>
    >>like if "a |b |c|" are the actual columns to be

    > displayed
    >>
    >>they r displayed as
    >>
    >>a|b|c|a|b|c|
    >>
    >>
    >>
    >>can anyone help me???/
    >>
    >>this is my onitemcommand code
    >>
    >>protected void Item_click(object source,
    >>System.Web.UI.WebControls.DataGridCommandEventArgs e)
    >>
    >>{
    >>
    >>int key = Convert.ToInt32 (DataGrid1.DataKeys

    > [e.Item.ItemIndex]);
    >>
    >>SqlDataReader read = project.GetSingleProject (key);
    >>
    >>if(read.Read ())
    >>
    >>{
    >>
    >>string name = read["ProjectName"].ToString ();
    >>
    >>Label1.Text = "Tasks Related to "+name;
    >>
    >>}
    >>
    >>TasksDB task=new TasksDB ();
    >>
    >>DataSet ds = task.GetTasks (key);
    >>
    >>DataGrid2.DataSource =ds;
    >>
    >>DataGrid2.AutoGenerateColumns =false;
    >>
    >>foreach(DataColumn c in ds.Tables [0].Columns )
    >>
    >>DataGrid2.Columns .Add (CreateBoundColumn(c));
    >>
    >>DataGrid2.DataBind ();
    >>
    >>}
    >>
    >>
    >>.
    >>
     
    Phani, Apr 27, 2005
    #3
  4. Phani

    Phani Guest

    This is my first datagrid .....

    <asp:datagrid id="DataGrid1" runat="server" Width="100%"
    AutoGenerateColumns="False" Font-Names="Tahoma" Font-Size="Smaller"
    AllowSorting="True" OnItemCommand="Item_click">
    <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White"
    VerticalAlign="Middle"
    BackColor="#6699CC"></HeaderStyle>
    <Columns>
    <asp:ButtonColumn Text="+" CommandName="SelectProject">
    <HeaderStyle Width="20px"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:ButtonColumn>
    </Columns>
    </asp:datagrid>

    This is my second datagrid ......

    <asp:DataGrid id="DataGrid3" runat="server" AutoGenerateColumns="False"
    Width="100%">
    <HeaderStyle Font-Size="Smaller" Font-Bold="True" ForeColor="White"
    BackColor="#660000"></HeaderStyle>
    </asp:DataGrid>

    And the code behind is as follows ....

    private void Page_Load(object sender, System.EventArgs e)

    {

    DataSet ds1 = project.GetProjects (Context.User.Identity.Name);

    DataGrid1.DataSource =ds1;

    DataGrid1.DataKeyField ="ProjectId";

    if(Context.User.IsInRole ("Members"))

    {

    foreach(DataColumn c in ds1.Tables [0].Columns )

    {

    if(c.ColumnName =="ProjectId" || c.ColumnName =="ProjectCreationDate" ||
    c.ColumnName =="ProjectCreatorId" || c.ColumnName =="StatusId")

    continue;

    DataGrid1.Columns .Add (CreateBoundColumn(c));

    }

    }

    else

    {

    foreach(DataColumn c in ds1.Tables [0].Columns )

    {

    DataGrid1.Columns .Add (CreateBoundColumn(c));

    }

    }

    DataGrid1.DataBind ();

    }

    private BoundColumn CreateBoundColumn(DataColumn c)

    {

    BoundColumn Column=new BoundColumn ();

    Column.DataField =c.ColumnName ;

    Column.HeaderText =c.ColumnName ;

    return Column;

    }

    protected void Item_click(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {

    int key = Convert.ToInt32 (DataGrid1.DataKeys[e.Item.ItemIndex]);

    SqlDataReader read = project.GetSingleProject (key);

    if(read.Read ())

    {

    string name = read["ProjectName"].ToString ();

    Label1.Text = "Tasks Related to "+name;

    }

    TasksDB task=new TasksDB ();

    DataSet ds = task.GetTasks (key);

    DataGrid3.AutoGenerateColumns =false;

    if(Context.User.IsInRole ("Members"))

    {

    foreach(DataColumn c in ds.Tables [0].Columns )

    {

    if(c.ColumnName =="TaskId" || c.ColumnName =="TaskCreationDate" ||
    c.ColumnName =="TaskCreatorId" || c.ColumnName =="StatusId")

    continue;

    BoundColumn Column=new BoundColumn ();

    Column.DataField =c.ColumnName ;

    Column.HeaderText =c.ColumnName ;

    DataGrid3.Columns .Add (Column);

    }

    }

    else

    {

    foreach(DataColumn c in ds.Tables [0].Columns )

    {

    BoundColumn Column=new BoundColumn ();

    Column.DataField =c.ColumnName ;

    Column.HeaderText =c.ColumnName ;

    DataGrid3.Columns .Add (Column);

    }

    }

    DataGrid3.DataSource =ds;

    DataGrid3.DataBind ();

    }



    And the problem is ...... the columns are displayed twice in the second
    datagrid ....

    someone help me out please ....

    eg; Actual columns to be displayed ..... A B C

    Displayed columns ....... A B C A B C



    regards

    Phani
     
    Phani, Apr 27, 2005
    #4
  5. Phani

    Elton W Guest

    Hi Phani,

    You can set breakpoint in code, and trace running to see
    what happens.

    HTH

    Elton


    >-----Original Message-----
    >This is my first datagrid .....
    >
    ><asp:datagrid id="DataGrid1" runat="server" Width="100%"
    >AutoGenerateColumns="False" Font-Names="Tahoma" Font-

    Size="Smaller"
    >AllowSorting="True" OnItemCommand="Item_click">
    > <AlternatingItemStyle

    BackColor="Gainsboro"></AlternatingItemStyle>
    > <HeaderStyle Font-Bold="True"

    HorizontalAlign="Center" ForeColor="White"
    >VerticalAlign="Middle"
    > BackColor="#6699CC"></HeaderStyle>
    > <Columns>
    > <asp:ButtonColumn Text="+"

    CommandName="SelectProject">
    > <HeaderStyle Width="20px"></HeaderStyle>
    > <ItemStyle HorizontalAlign="Center"
    >VerticalAlign="Middle"></ItemStyle>
    > </asp:ButtonColumn>
    > </Columns>
    > </asp:datagrid>
    >
    >This is my second datagrid ......
    >
    > <asp:DataGrid id="DataGrid3" runat="server"

    AutoGenerateColumns="False"
    >Width="100%">
    > <HeaderStyle Font-Size="Smaller" Font-Bold="True"

    ForeColor="White"
    >BackColor="#660000"></HeaderStyle>
    > </asp:DataGrid>
    >
    >And the code behind is as follows ....
    >
    >private void Page_Load(object sender, System.EventArgs e)
    >
    >{
    >
    >DataSet ds1 = project.GetProjects

    (Context.User.Identity.Name);
    >
    >DataGrid1.DataSource =ds1;
    >
    >DataGrid1.DataKeyField ="ProjectId";
    >
    >if(Context.User.IsInRole ("Members"))
    >
    >{
    >
    >foreach(DataColumn c in ds1.Tables [0].Columns )
    >
    >{
    >
    >if(c.ColumnName =="ProjectId" || c.ColumnName

    =="ProjectCreationDate" ||
    >c.ColumnName =="ProjectCreatorId" || c.ColumnName

    =="StatusId")
    >
    >continue;
    >
    >DataGrid1.Columns .Add (CreateBoundColumn(c));
    >
    >}
    >
    >}
    >
    >else
    >
    >{
    >
    >foreach(DataColumn c in ds1.Tables [0].Columns )
    >
    >{
    >
    >DataGrid1.Columns .Add (CreateBoundColumn(c));
    >
    >}
    >
    >}
    >
    >DataGrid1.DataBind ();
    >
    >}
    >
    >private BoundColumn CreateBoundColumn(DataColumn c)
    >
    >{
    >
    >BoundColumn Column=new BoundColumn ();
    >
    >Column.DataField =c.ColumnName ;
    >
    >Column.HeaderText =c.ColumnName ;
    >
    >return Column;
    >
    >}
    >
    >protected void Item_click(object source,
    >System.Web.UI.WebControls.DataGridCommandEventArgs e)
    >
    >{
    >
    >int key = Convert.ToInt32 (DataGrid1.DataKeys

    [e.Item.ItemIndex]);
    >
    >SqlDataReader read = project.GetSingleProject (key);
    >
    >if(read.Read ())
    >
    >{
    >
    >string name = read["ProjectName"].ToString ();
    >
    >Label1.Text = "Tasks Related to "+name;
    >
    >}
    >
    >TasksDB task=new TasksDB ();
    >
    >DataSet ds = task.GetTasks (key);
    >
    >DataGrid3.AutoGenerateColumns =false;
    >
    >if(Context.User.IsInRole ("Members"))
    >
    >{
    >
    >foreach(DataColumn c in ds.Tables [0].Columns )
    >
    >{
    >
    >if(c.ColumnName =="TaskId" || c.ColumnName

    =="TaskCreationDate" ||
    >c.ColumnName =="TaskCreatorId" || c.ColumnName

    =="StatusId")
    >
    >continue;
    >
    >BoundColumn Column=new BoundColumn ();
    >
    >Column.DataField =c.ColumnName ;
    >
    >Column.HeaderText =c.ColumnName ;
    >
    >DataGrid3.Columns .Add (Column);
    >
    >}
    >
    >}
    >
    >else
    >
    >{
    >
    >foreach(DataColumn c in ds.Tables [0].Columns )
    >
    >{
    >
    >BoundColumn Column=new BoundColumn ();
    >
    >Column.DataField =c.ColumnName ;
    >
    >Column.HeaderText =c.ColumnName ;
    >
    >DataGrid3.Columns .Add (Column);
    >
    >}
    >
    >}
    >
    >DataGrid3.DataSource =ds;
    >
    >DataGrid3.DataBind ();
    >
    >}
    >
    >
    >
    >And the problem is ...... the columns are displayed twice

    in the second
    >datagrid ....
    >
    >someone help me out please ....
    >
    >eg; Actual columns to be displayed ..... A B C
    >
    >Displayed columns ....... A B C A B C
    >
    >
    >
    >regards
    >
    >Phani
    >
    >
    >.
    >
     
    Elton W, Apr 27, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. helpful sql
    Replies:
    0
    Views:
    830
    helpful sql
    May 19, 2005
  2. Replies:
    0
    Views:
    609
  3. Replies:
    5
    Views:
    612
  4. boney
    Replies:
    1
    Views:
    580
  5. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    410
    Kiuhnm
    Apr 1, 2006
Loading...

Share This Page