I haven't created my own control before but it's got to the point where I think I need to, I need to be able to create a datagrid where I can add different controls to the fields and creating template columns dynamically isn't quite cutting it, what is the
best way to start looking into this?
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentShip As String = DRV("ShipVia")
Dim DDL As DropDownList = _
CType(e.Item.Cells(4).Controls(1), DropDownList)
Dim SQL As String = _
"SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub
breath2k
Contributor
2101 Points
821 Posts
Creating a custom DataGrid
Sep 20, 2011 10:17 AM|LINK
I haven't created my own control before but it's got to the point where I think I need to, I need to be able to create a datagrid where I can add different controls to the fields and creating template columns dynamically isn't quite cutting it, what is the best way to start looking into this?
chandrasheka...
Star
10258 Points
1760 Posts
Re: Creating a custom DataGrid
Sep 20, 2011 10:27 AM|LINK
Refer the following link in which the user has created a custom grid extending the DataGrid control in asp.net
http://blogs.msdn.com/b/paraga/archive/2005/11/26/497137.aspx
http://www.codeproject.com/KB/aspnet/Extending_the_datagrid.aspx
http://www.codeproject.com/KB/webforms/datagrid_extend_functions.aspx
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Creating a custom DataGrid
Sep 22, 2011 01:44 AM|LINK
Hello breath2k,
If you want to add several (I mean "limited" controls). You don't need to extend it but just handle ItemDataBound or some other events to do this:
More info at: http://msdn.microsoft.com/en-us/library/aa479316.aspx, here's the sample from there:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _

ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentShip As String = DRV("ShipVia")
Dim DDL As DropDownList = _
CType(e.Item.Cells(4).Controls(1), DropDownList)
Dim SQL As String = _
"SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub