Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the structure of a DataGrid In Edit Mode 1

Status
Not open for further replies.

Modica82

Technical User
Jan 31, 2003
410
GB
Hi all,

I have a datagrid, and when i click the edit record button i want the dataGrid to change the structure slightly. For example i have:

M - T - W - T - F

in Edit:

i just want one big column. If not, i think i am gonna have to do the edit in a pop_up.

Any help would be great.

Rob
 
Sure you can remove the colums.

Trap the event of creating the row (DataGridItem) using either the OnItemCreated event or the OnItemDataBound event. Check the ListItemType fo the current item. If it's a EditeItemType, you knwo you're on the row being edited. Then its a simple matter to clear out the extra cells and then change the remaining cells colspan attribute so that it fills up the remaining cells.

Here's a little method I wrote which will aloow you to convert a DataGridItem (basically a row in a table) to having just one large cell whose colspan matches the count of the columns in the grid
Code:
Public Shared Sub MakeRowSingleCell(ByVal dataGridItem As System.Web.UI.WebControls.DataGridItem)
            Dim originalCellCount As Int32 = dataGridItem.Cells.Count
            dataGridItem.Cells(0).ColumnSpan = originalCellCount
            Dim x As Int32 = originalCellCount - 1
            While (x > 0)
                dataGridItem.Cells.RemoveAt(x)
                x -= 1
            End While
        End Sub

use it during the item created event like this
Code:
protected Sub DataGrid1_OnItemCreated(sender as Object, e as DataGridItemEventArgs)

If e.Item.ItemType.Equals(ListItemType.EditItem) Then

   MakeRowSingleCell(e.Item)
End IF

end sub

Greetings,
Dragonwell
 
Hi Dragonwell,

thanks for the code! I am a little stuck at the moment and was wondering if you could help me. I have adjusted your code to fit in with my situation(not much mind ;)) but i have a problems. The code does what it is supposed to do and expands the column to make one large one, but it also gets rid of my last edit column which houses my save and cancel buttons for that record. Instead i get a 0 :S

Any Ideas?

Rob
 
Try this:
Code:
 Public Shared Sub TrimToCellCount(ByVal dataGridItem As System.Web.UI.WebControls.DataGridItem, ByVal desiredCount As Int32)
            Dim originalCellCount As Int32 = dataGridItem.Cells.Count

            Dim counter As Int32 = 0
            Dim x As Int32 = originalCellCount - 1
            While (x > (desiredCount - 1))
                dataGridItem.Cells.RemoveAt(x)
                counter += 1
                x -= 1
            End While

            dataGridItem.Cells((desiredCount - 1)).ColumnSpan = counter + 1
            dataGridItem.Width = System.Web.UI.WebControls.Unit.Percentage(100)
        End Sub

This will let you specify how many cells you want to end up with. I fyou want just two columns, call it like this:
Code:
TrimToCellCount(e.Item, 2)

Greetings,
Dragonwell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top