So just a quick post about some more datagrid fun from yesterday before I really set into the day’s work in earnest. Basically adding other controls to a datagrid (in this case a textbox and a dropdownlist) so that the user can just edit all the fields at once similar to how excel works. So to start here is the end result screen shot:

So from there, onto the code. It works pretty much the same as the one in the previous article, using a template column for the controls.

DropDownList:

<asp :templatecolumn></p>

  <headertemplate>Vendor</headertemplate>
  <itemtemplate>
    <asp :dropdownlist ID=”ddlVendor” CssClass=”dgddl” Runat=”server”
      DataTextField=”VendorDisplay” DataValueField=”VendorID”
      DataSource=”<%# LoadVendors() %>”>
    </asp>
  </itemtemplate>
</asp>

TextBox:

<asp :templatecolumn></p>

  <headertemplate>Actual Cost</headertemplate>
  <itemtemplate>
    <asp :textbox ID=”txtActualCost” CssClass=”shorttextbox”
      Runat=”server”
      Text=’<%#DataBinder.Eval(Container.DataItem, "ActualCost")%>’>
    </asp>
  </itemtemplate>
</asp>

They pretty much follow the same pattern, but the dropdownlist has a few more things to it, namely DataTextField and DataValueField. The first time I created the DDL I just called the onLoad and past it a method that would populate each individually, and that worked fine for populating them, but it gave me errors when I went to retrieve the values selected by the user so I opted for this method instead which I believe is probably the better method. The DateSource points to the LoadVendors Function which returns a DataTable.

For the text box you just have to point it to the right field and it takes care of the rest for you.

So to start with the code portion of this, let’s look at the LoadVendors, and then we’ll take a look at populating the rest of the DataGrid.

Dim rSet As SqlDataReader
Dim dt As New DataTable
dt.Columns.Add(New DataColumn(“VendorID”))
dt.Columns.Add(New DataColumn(“VendorDisplay”))
...
rSet = cmd.ExecuteReader()
Dim dr As DataRow = dt.NewRow
dr(0) = -1
dr(1) = “”
dt.Rows.Add(dr)
Do While rSet.Read
  dr = dt.NewRow
  dr(0) = rSet(“VendorID”)
  dr(1) = rSet(“VendorTradeName”) & ” – ” & rSet(“City”) & ”, ” & rSet(“State”)
  dt.Rows.Add(dr)
Loop
rSet.Close()
LoadVendors = dt

So basically I’m just creating the DataTable here with two columns, which if you’ll scroll up and look, correspond to the DataTextField and the DataValueField and I just fill them from there. So now we have this nice dropdownlist, but it’s rather useless if it all it does is force the person to pick their things everytime and it makes for a real pain to code if the person is changing that everytime so I need to pull back the data and pre-select a field if one has been selected before. This I put into the populate method for the entire DataGrid so that it happens right after the Datagrid has been bound. What I’m doing is quite simple, and there may be a better way, because as my friend puts it, I’m just cheating at Scrabble at this point. Namely I’m storing the value of that dropdownlist in the datagrid itself, which lets me set the DDL after binding, and it also stores the previously selected value for me so that I don’t update when I don’t need to.

Dim dt As New DataTable
...
If Not IsNothing(dt) And dt.Rows.Count > 0 Then
  Me.dgItenerary.DataSource = dt
  Me.dgItenerary.DataBind()
  For Each item As DataGridItem In Me.dgItenerary.Items
    If (item.Cells(6).Text <> 0) Then
      Dim ddl As New DropDownList
      ddl = CType(item.Cells(3).FindControl(“ddlVendor”), DropDownList)
      ddl.SelectedValue = item.Cells(6).Text
    End If
  Next
End If

So basically I’m just running through each item in the datagrid and seeing if I had a value for that VendorID stored in there because sometimes a NULL gets returned and that’s quite alright. I also didn’t see a problem with just running it through a for loop in this instance because I never have more than 10 things at a time in this particular datagrid so I’m not taking a real performance hit. Not sure if it would be a problem with a large datagrid, but I’d rather try and figure out a way to handle it in the first pass instead. For that more than likely you’d just throw something into the SelectedIndex attribute of the control. Anyways I digress.

So what I’m doing is just looping through, if I have that VendorID then I want to pre-select the value of the DropDownList. I do this by finding the DDL control inside the Cell and casting it to a variable I can work with, set the SelectedValue and then move on.


No Responses to “More DataGrid Fun, adding other controls”  

  1. No Comments

Leave a Reply