Remove a col from datagrid before binding

eramgarden

Contributor
Joined
Mar 8, 2004
Messages
579
I have a datagrid, with 4 columns.

In the code behind, i want , based on a condition, to remove 2 of the columns before binding..

I tried the following:
Code:
 dg.DataSource = myDS 'bind dataset to the datagrid

 [b] dg.FindControl("lblDesc").visible = false
    ' Also tried the one below
  dg.items.item(1).visible = false [/b]

            dg.DataBind()

and this is the itemtemplate i have:

Code:
<tr>
												<td align="left"><b>Status:</b></td>
												<td>
													<asp:Label ID="lblDesc" Runat=server text='<%# Container.DataItem("incid_status_desc") %>'>
													</asp:Label>
												</td>
											</tr>

How can I remove a col from a datagrid??
 
Are you dynamically creating the columns or have you defined them at design-time? The difference in code is only in where you place it. The code you are really looking for is:

DataGrid1.Columns(#ofColumntoMakeInvisible).Visible = False

If you are dynamically creating your columns you won't be able to do this until after the databind. Otherwise, where you have it is fine.

HTH

Eva
 
I have the columns set at desgin time...

It didn't work. None of the columns show up!

i also tried dg.columns(1).visible.false
What am I missing??

Code:
dg.DataSource = myDS 'bind dataset to the datagrid
           [b] dg.Columns("lblDesc").Visible = False
            dg.Columns("lblAssgined").Visible = False[/b]
            dg.DataBind()
 
Oh, i'm so stupid :-)..

I do NOT want to remove a col...

I have ONE column, it has 4 rows in it..want to remove a row from that column..

I tried this but i still can see the row:

Code:
Dim myDS As DataSet = New DataSet()
        Dim myDA As SqlDataAdapter = New SqlDataAdapter()

        myDA.SelectCommand = cmd



        myDA.Fill(myDS, "test")  ' bind to dataset
       ' Dim test1 = myDS.Tables("test").Rows(1).Item(1)
      [b]  myDS.Tables("test").Rows(1).Delete()[/b]

Dim test1 = myDS.Tables("test").Rows(1).Item(1)...
Gives me the second row but i want to remove it and not bind it to my datagrid for certain conditions..
 
It sounds like your best bet is not to include it in your selection. I would write my stored proc or my SQL statement to only select the items I wanted to appear in the datagrid. This would be the fastest way to do this.
 
No, this is not the solution :(

I have 2 modes: Mode1, Mode2.

I have 2 SQLs based on the mode: One sql brings 4 values, another one brings 2 values...

I have a dataGrid with 1 column, 4 rows in it for Mode1.

For Mode2, I want my dataGrid to STILL have 1 column and have ONLY 2 rows ...

Somehow, i want to use the SAME dataGrid and delete the 2 extra rows for Mode2...

My solution was this: Use 2 dataGrids, based on Mode, display the right dataGrid...But i had to use lots of If-Else to display the right datagrid...

There has to be a better way than what I did, couldnt use panels with dataGrid, but there has to be a way to delete 2 rows from DataGrid...
 
i think you are looking at it the wrong way.. don't think "delete", think "hide"

Bind the grid with all 4 columns, and wire into the "OnItemDataBound" event of the DataGrid and set e.Item.Cells(Index).Visible = False
 
Back
Top