eramgarden Posted April 19, 2004 Posted April 19, 2004 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: 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: <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?? Quote
evaleah Posted April 19, 2004 Posted April 19, 2004 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 Quote
eramgarden Posted April 19, 2004 Author Posted April 19, 2004 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?? dg.DataSource = myDS 'bind dataset to the datagrid [b] dg.Columns("lblDesc").Visible = False dg.Columns("lblAssgined").Visible = False[/b] dg.DataBind() Quote
MorningZ Posted April 19, 2004 Posted April 19, 2004 one problem is that you have to set them to Visible = False after you find the data Quote If you make it idiot proof, they'll build a better idiot :-)
eramgarden Posted April 19, 2004 Author Posted April 19, 2004 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: 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.. Quote
evaleah Posted April 19, 2004 Posted April 19, 2004 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. Quote
eramgarden Posted April 20, 2004 Author Posted April 20, 2004 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... Quote
MorningZ Posted April 20, 2004 Posted April 20, 2004 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 Quote If you make it idiot proof, they'll build a better idiot :-)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.