How to remove a column from GridView

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. 

0
akkeee
7/26/2008 11:53:40 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

5 Replies
696 Views

Similar Articles

[PageSpeed] 25

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


Techie (.Net Knowledge at its Best)


We make a living by what we get, but we make a life by what we give.
::: Winston Churchill :::
0
PassHours
7/26/2008 3:06:59 PM

I do this at the end of Databound Event and works

 

  GridView1.Columns[n].Visible = false;


Keep it Simple!!!
DRY
YAGNI
Last Responsible Moment

If you like my answer don't forget to mark it.
0
dacanetdev
7/26/2008 3:20:44 PM

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.

0
akkeee
7/27/2008 7:13:08 AM

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.

0
akkeee
7/27/2008 7:21:32 AM

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;

}

0
akkeee
7/28/2008 1:15:38 PM
Reply:

Similar Artilces:

remove a gridview column dynamically, removes controls in template columns
I have a interface with a treeview and a gridview. The gridview has two templatefields that handles insert/update/deletes. The gridview is handling editing of different tables so I create the other columns in the gridview dynamically as boundfields. The user selects the table to edit in the treeview. The problem I am having happens when the user selects a table to edit the second time. The code that executes removes all bound fields, (dynamically added when user selected the first table in the treeview), but not the two template fields, and then adds boundfields specifi...

Gridview control removing consecutive spaces in column output
 Hi, i am using a simple GridView to retrieve data from a SQL view, and if there are consecutive spaces in the string in a particular column, something (asp?) is removing the 2nd space, and then the output has only 1 space, which is incorrect for this string. Interestingly, if I retrieve the same column with a specific select statement in a code behind file, and then output it via either response.write, or in a text box, it does the same thing.   I have debugged while doing this and in the Autos window, the string is correct (with two consecutive spaces), then when it is ...

Binding GridView Columns with controls outside the GridView
HiI already posted this issue but couldn't get the proper resolution so making it more clear;Problem is my controls are outside the grid. Like ive text boxes, for First Name and Last Name and dropdown for country. User can fill these fields for entering a new record and also update the existing by selecting it from the grid.Like user selects a row from the grid and its contents will be displayed on the textboxes and drop downs that are out side the grid.Hope i could explain it better now.  RegardsMohsin Shahzad You could use a DetailsView or FormView control that is linked to...

How can I control the width of a column in the GridView control.
From searching I see mention of ways to do it before the controls are bound. Is there a simpler way? If not, could someone explain to me how I need to go about it?Here is the code: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"        DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."        OnSelectedIndexChanged="GridView1_SelectedIndexChanged">   &nbs...

Is there a control that offers the same data presentation as a GridView, but allows users to copy and paste many rows/columns at once?
 Hi Is there a control that offers the same data presentation as a GridView, but allows users to copy and paste many rows/columns at once onto the grid? What I mean is some grid that 's as flexible as MS Excel grid for example.  Any suggestions will be very much appreciated.E The Repeater control is a highly customisable data presentation controlRohin.NET developerNever trust a computer you can't throw out a window. - Steve WozniakPlease remember to click on 'Mark as answer', if this post helped you......

gridview control in another gridview control
Hi ia have a questioni have a gridview control which contains button in one colum.now my question is i need another gridview to be placed in the  first gidviewso that when i click on the button the second gridview will populate the data.now my question is how to place second gridview in first gridview.or is there any approach like first grid itself acting as parent and child.please help me  Hi sirikalavalapalli:                I think nested gridview will help you. Check this link and it will show you how...

Help Req: !!! :- On Disabling Controls Present inside a Gridview control
Hi All, We have A requirement Where we have disable controls inside a grid.These controls can be textbox, DropDownlist (in TemplateField), ComandField present inside the grid.We have to call a generic method for doing the same .(since it can be done for multiple grid.) PFB the code snippet we use. Which disables all the controls inside the page.The bolded part of the code is used to loop inside a grid view.(Not Working.)Is this ideal way to deal with the requirement.Or can someone suggest be a better way.        public void DisableMe(Page p1, st...

Gridview control embedded in another gridview control
I am writing an ambulance crew scheduling application on the web and I need some help completing the last part.  I have an access database that has three tables: user table, shift table (describing the shifts available), and a schedule table (that stores shift by shift_id and user by user_id).  I have a gridview control that will display the available shifts but I cannot get the embedded gridview control that will display the user that is signed up for each shift to display because I cannot figure out how to use the OnRowDataBound method.  I need a simple VB example of how to...

Disable button server control under GridView when one of the Column in Gridview Total is Zero
Hi web page(aspx) consist of a GridView with two columns displayed from Database. GridView has two columns 1)Employees_Department and 2)Total_Employees. In Employees_Department column employee department name is retrieved and displayed as a Hyperlink when Total_Employees column value is not Zero(This is achieved using row_databound event code shown below). Clicking or selecting particular Employees_Department row will open data for that Employees_Department in a new window. In Total_Employees column of the GridView, total number of employees in that(row) department displayed. GridView is ...

How to manipulate images and combe multiple database columns into one column in a GridView control
Hi Folks,I'm hoping someone can help me out with something.I am trying to put together a photo album as a learning exercise and have a GridView control with two columns linked to an Access Data Source. In the left hand column (called Image) I want to display the photo and in the right hand column (called Details) I want to provide details such as Date, Location, People and Comments, Camera Type etc. Each of these is columns is taken from the database I have set up.However, I have am having some problems:1: At the moment I have been able to get the image to display (using an image column ...

Issue with Template Columns & Bound Columns while updating the data in a GridView control
Hi, I'm facing the below problem (In general, ObjectDataSource is driving me crazy ! ) I have a simple gridview control and an ObjectDataSource is used to populate the grid. I'm trying to update a row and the corresponding tableadapter methods & stored procedure are in place. Each row has 2 checkboxes and 2 textboxes in edit mode. When the change the values in edit mode and hit update, the checkbox values changed are saved into the Database properly, whereas the textbox values are not being saved. If the textbox column is a bound column then the column is updated with <NULL> in...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Master Details in two seperate GridView Controls
I am simply trying to have a list of people in the top grid control (works fine), then when selected have the second Grid Control show details for that person(not working). Here is what I am trying from the load event: Sub BindData() Dim myConnection As New MySql.Data.MySqlClient.MySqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionSql")) Dim strPersonSQL, strActSQL As String strPersonSQL = "select row_id, first_name, last_name, middle_initial, dsi_id, created, created_by, program_of_study, email from tbl_person" Dim myPerCommand As New MySqlClient.MySqlComm...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

Web resources about - How to remove a column from GridView - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 12/4/2015 7:14:40 PM