locked
How to remove a column from GridView RRS feed

  • Question

  • User-21846311 posted

    Hi Friends,  I have a DataTable with 2 columns (ID,  SomeData) that is I am getting from my DataBase, now I want to show in the gridview only two columns (Name, SomeData), Name I am getting from some web service, code is something like this.

    Button_Click(…..)
    {
      Datatable dt;
      dt = getMyTable();
      dt.Columns.Add("Name", typeof(string));
      dt.Columns["Name"].SetOrdinal(1);
      myGridView.DataSource = dt;
      myGridView.DataBind();
    }

    myGridView_RowDataBound(…)
    {               
      e.row.cells["Name"].Text = myWebService.getEmployeeName(Convert.Int32(e.Row.Cell["ID"].Text)               
      e.row.cells["SomeData"].Text = “<a href=’MoreDetails.aspx?ID=” + e.Row.Cell["ID"].text + ”’>” + e.Row.Cell["SomeData"].Text + “</a>”;
    }

     
    Problem is I want to show ID column. I can’t remove the ID column from datatable before databinding (coz I need that column for anchor tag on SomeData column). And I am getting exception if i try to set that columns visible property to false after binding (column dosnt exist) . Please suggest.Thank you in advance. 

    Saturday, July 26, 2008 7:53 AM

Answers

  • User-21846311 posted

    i was not able to hide column so i hide the cell, at the time of RowDataBound

    myGridView_RowDataBound(…)
    {               
      e.row.cells["Name"].Text = myWebService.getEmployeeName(Convert.Int32(e.Row.Cell["ID"].Text)               
      e.row.cells["SomeData"].Text = “<a href=’MoreDetails.aspx?ID=” + e.Row.Cell["ID"].text + ”’>” + e.Row.Cell["SomeData"].Text + “</a>”;
      e.row.cells["ID"].visible = false;

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 28, 2008 9:15 AM

All replies

  • User1710623990 posted

    Don't bind the ID column with the grid.

    Use the grid property DataKeyNames="ID" in <asp:GridView........... />

    Visit Gridview Hidden Column for the usage 

    Hope that helps [Yes]

    Saturday, July 26, 2008 11:06 AM
  • User-1318567937 posted

    I do this at the end of Databound Event and works

     

      GridView1.Columns[n].Visible = false;

    Saturday, July 26, 2008 11:20 AM
  • User-21846311 posted

    thanks for reply, but I can't use TemplateField i didnt' mention one thing that is my second column is dynamic mean some times i get my table like this (ID, SomeData1) next time may be table like this (ID, SomeData1, SomeData2) next time (ID, SomeData1, SomeData2, SomeData3) so the problem is my number of columns are dynamic based on the selected option of user.

    Sunday, July 27, 2008 3:13 AM
  • User-21846311 posted

    Hi dacanetdev,

    I mentioned earlier that I tried to set that columns visible property to false after binding but I am getting exception column doesn’t exist.

    Sunday, July 27, 2008 3:21 AM
  • User-21846311 posted

    i was not able to hide column so i hide the cell, at the time of RowDataBound

    myGridView_RowDataBound(…)
    {               
      e.row.cells["Name"].Text = myWebService.getEmployeeName(Convert.Int32(e.Row.Cell["ID"].Text)               
      e.row.cells["SomeData"].Text = “<a href=’MoreDetails.aspx?ID=” + e.Row.Cell["ID"].text + ”’>” + e.Row.Cell["SomeData"].Text + “</a>”;
      e.row.cells["ID"].visible = false;

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 28, 2008 9:15 AM