Gridview binding with combobox's in header - asp.net

Could anyone please enlighten me about how one might go about binding to a gridview in ASP.Net 4 in a scenario where the first row of my gridview should be the headers, the second should be a combobox for each column and the third is the beginning of my actual datasource.
If you can imagine what I am trying to achieve is an ability to create a binding between each column in the datagrid and another datasource. This binding is created by the user selecting a value in the comboboxes. However no matter what I try I cant seem to achieve this.
HeaderText1 | HeaderText2 | HeaderText3
ComboBox1 | ComboBox2 | ComboBox3
DataRow1 | DataRow1 | DataRow1
DataRow2 | DataRow2 | DataRow2
DataRow3 | DataRow3 | DataRow3

You can put a DropDownList into a Gridview column quite easily by using a TemplateColumn:
<asp:GridView runat="server" ID="ComboboxGridView">
<Columns>
<asp:TemplateField HeaderText="Column 1">
<HeaderTemplate>
<asp:DropDownList runat="server" ID="Column1DropDownList" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="Column1DisplayLabel" Text='<%# Eval("Column1") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and you can bind the DropDownList to another data source quite easily, especially you're using the DataSource controls. I'm not clear on what you're doing with the DropDownLists in the header though - is it for filtering the rows that appear in the GridView?

So for anyone curious this appears to be the solution to the problem.
Private Sub grdMainGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdMainGrid.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
For Each itm As TableCell In e.Row.Cells
itm.Text = GenerateHeaderHTML()
Next
End If
End Sub
PS: If anyone has any better solutions I would love to hear them :-)
The following is the code I have in the GenerateHeaderHTML(). My code is a very specific case (and prob far from great). However note that you can use any html you wish.
Private Sub grdMainGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdMainGrid.RowCreated
If Me.BoundedObjects IsNot Nothing Then
If e.Row.RowType = DataControlRowType.Header Then
Dim PrimitivePropertyNames As List(Of String) = ParserHelper.GetPrimitivePropertyNames(Me.BoundedObjects.ToList)
Dim i As Integer = 0
For Each itm As TableCell In e.Row.Cells
itm.Text = ucStockImport.CreateBindingHeaderTable(itm.Text, PrimitivePropertyNames, i.ToString)
i += 1
Next
End If
Else
Throw New StockImportException("ucStockImport.BoundedObjects Is Nothing")
End If
End Sub
Private Shared Function CreateBindingHeaderTable(ByVal HeaderText As String, ByVal PropertyNames As List(Of String), ByVal ID As String) As String
Return String.Format("<table><tr><td>{0}</td></tr><tr><td>{1}</td></tr></table>", HeaderText, ucStockImport.CreateBindedObjectDropDownList(PropertyNames, ID))
End Function
Private Shared Function CreateBindedObjectDropDownList(ByVal PropertyNames As List(Of String), ByVal ID As String) As String
Dim strBuilder As New StringBuilder
strBuilder.Append(String.Format("<option value=""{0}"">{1}</option>", i, propName))
Dim i As Integer = 0
For Each propName As String In PropertyNames
strBuilder.Append(String.Format("<option value=""{0}"">", i) & propName & "</option>")
i += 1
Next
strBuilder.Append("</select>")
Return strBuilder.ToString
End Function

Related

ChekBoxList Not Noticing Selected, and not displaying value

Not picking up that something is selected, and not displaying the value.
When I debug the code and step-in:
VBCODE:
When Debugging the "li" after Each holds a value(ex286)
when I go to Item and open the box I get this:
Item = Argument not specified for parameter 'index' of 'Public ReadOnly Default Property Item(index As Integer) As System.Web.UI.WebControls.ListItem'.
Item = In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
the "li" after If holds a value(ex286), but the Selected is "FALSE" Do not know why.
After the = li is the text and the Value(286)
Another thing it only gives me the value for the first box value not the rest if I click them.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
For Each li As ListItem In CheckBoxList.Items
If li.Selected Then
Texttext.Text = li.Value
Else
Texttext.Text = "Give Up Loser!"
End If
NextEnd Sub
ASCX FILE
<asp:CheckBox ID="CheckBoxSelectAll" runat="server" Text="Select All" AutoPostBack="True" />
<asp:CheckBoxList ID="CheckBoxList" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="Name" DataValueField="Id"
RepeatColumns="3" ></asp:CheckBoxList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStuff"
DataObjectTypeName ="DataTransfer.TheData"
TypeName="BusinessDelegate.DataBusinessDelegate">
</asp:ObjectDataSource>
<asp:LinkButton ID="LinkButton" runat="server" Text="Here"></asp:LinkButton>
<asp:Label ID ="Texttext" runat="server" Text=""></asp:Label>
I have tried a few items from online but nothing worked correctly.
Get all selected values of CheckBoxList in VB.NET
ASP.NET, VB: checking which items of a CheckBoxList are selected
I am not sure what you are trying to achieve with this.
The below code might work for you.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Dim cbChecked As Boolean
For lItem = 0 To CheckBoxList.Items.Count - 1
cbChecked = CheckBoxList.GetItemChecked(lItem)
If cbChecked Then
Texttext.Text = CheckBoxList.GetItemText(lItem)
Else
Texttext.Text = "Give Up Loser!"
End If
Next
End Sub
When you run this above code, if the last check box is not checked then you will end up with
'Give Up Loser!' in the 'Texttext' text box.
You can get the number of checked boxes in the list by using the below code
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Dim checkedBoxes = CheckBoxList.CheckedItems
Dim checkedBoxesCount = checkedBoxes.Count
For Each lItems In checkedBoxes
Dim chkdCheckBoxName = lItems.ToString
Next
End Sub
Try this below code to write the values of checked boxes in the text box.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Texttext.Text = "" 'clearing the text box
Dim checkedBoxes = CheckBoxList.CheckedItems
Dim checkedBoxesCount = checkedBoxes.Count
For Each lItems In checkedBoxes
Dim chkdCheckBoxName = lItems.ToString
Texttext.Text = Texttext.Text & " | " & chkdCheckBoxName
Next
If checkedBoxesCount = 0 Then
Texttext.Text = "Give Up Loser!"
End If
End Sub
Above code is based on System.Windows.Forms.CheckedListBox

Two-way data binding in ASP.NET

Trying to use data binding between a list of objects and a data list control. What I want to do are
create the list of objects
have them bound to the controls
change data in the UI
have the changes in the ui bound to the list of objects
on post back - have the list of objects with the new values from the ui
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" DataKeyField="ClassID" ViewStateMode="Enabled">
<ItemTemplate>
<asp:TextBox ID="txtValue1" runat="server" Text='<%# Bind("Value1") %>'></asp:TextBox>
<asp:TextBox ID="txtValue2" runat="server" Text='<%# Bind("Value2") %>'></asp:TextBox>
<asp:TextBox ID="txtvalue3" runat="server" Text='<%# Bind("Value3") %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnDoPostBack" runat="server" Text="Do Post Back" />
</div>
</form>
</body>
Option Explicit On
Option Strict On
Imports System.Diagnostics
Partial Class _Default
Inherits System.Web.UI.Page
Dim Class1List As List(Of Class1)
Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
Dim txtValue1 As TextBox
Dim txtValue2 As TextBox
Dim txtValue3 As TextBox
Dim ItemIndex As Integer = 0
If Page.IsPostBack Then
Class1List = CType(Session("Class1List"), List(Of Global.Class1))
'Class1List = CType(DataList1.DataSource, List(Of Global.Class1))
For Each myDataListItem As DataListItem In DataList1.Items
txtValue1 = CType(myDataListItem.FindControl("txtValue1"), TextBox)
Long.TryParse(txtValue1.Text, Class1List(ItemIndex).Value1)
txtValue2 = CType(myDataListItem.FindControl("txtValue2"), TextBox)
Integer.TryParse(txtValue2.Text, Class1List(ItemIndex).Value2)
txtValue3 = CType(myDataListItem.FindControl("txtValue3"), TextBox)
Class1List(ItemIndex).Value3 = txtValue3.Text
ItemIndex += 1
Next
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myClass1 As Class1
If Not Page.IsPostBack Then
Class1List = New List(Of Class1)
myClass1 = New Class1
Class1List.Add(myClass1)
BindData()
Else
'Class1List = CType(DataList1.DataSource, List(Of Global.Class1))
Debug.WriteLine("Page_Load, Value1 = " & Class1List(0).Value1.ToString())
Debug.WriteLine("Page_Load, Value2 = " & Class1List(0).Value2.ToString())
Debug.WriteLine("Page_Load, Value3 = " & Class1List(0).Value3)
End If
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
Session("Class1List") = Class1List
End Sub
Sub BindData()
DataList1.DataSource = Class1List
DataList1.DataBind()
End Sub
Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
Dim myClass1 As Class1
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
myClass1 = CType(e.Item.DataItem, Class1)
Debug.WriteLine("DataList1_ItemDataBound, Value1 = " & myClass1.Value1.ToString())
Debug.WriteLine("DataList1_ItemDataBound, Value2 = " & myClass1.Value2.ToString())
Debug.WriteLine("DataList1_ItemDataBound, Value3 = " & myClass1.Value3)
End If
End Sub
Protected Sub btnDoPostBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDoPostBack.Click
Dim myRandom As New Random
Class1List(0).Value1 = myRandom.Next(100)
Class1List(0).Value2 = myRandom.Next(100)
Class1List(0).Value3 = myRandom.Next(100).ToString()
Debug.WriteLine("btnDoPostBack_Click, Value1 = " & Class1List(0).Value1.ToString())
Debug.WriteLine("btnDoPostBack_Click, Value2 = " & Class1List(0).Value2.ToString())
Debug.WriteLine("btnDoPostBack_Click, Value3 = " & Class1List(0).Value3)
BindData()
End Sub
End Class
The Class Class1 is trivial:
Option Explicit On
Option Strict On
Imports Microsoft.VisualBasic
Public Class Class1
Private _ClassID As Long
Private _Value1 As Long
Private _Value2 As Integer
Private _value3 As String = String.Empty
Public Property ClassID As Long
Get
Return _ClassID
End Get
Set(ByVal value As Long)
_ClassID = value
End Set
End Property
Public Property Value1 As Long
Get
Return _Value1
End Get
Set(ByVal value As Long)
_Value1 = value
End Set
End Property
Public Property Value2 As Integer
Get
Return _Value2
End Get
Set(ByVal value As Integer)
_Value2 = value
End Set
End Property
Public Property Value3 As String
Get
Return _value3
End Get
Set(ByVal value As String)
_value3 = value
End Set
End Property
End Class
Update: I got the code behind above to do what I want it to do - I was thinking there was a better way?
You didn't show your databinding "Load" phase (the code which binds the data from the list to the controls)--so I assume the part you are unhappy with is the "Save" phase (the code in Page_PreLoad which binds the modified values from the controls back to the list), i.e. #4 in your list:
have the changes in the ui bound to the list of objects
It sounds like you want "two-way Data Binding": you want .NET to update your model as easily as it reads from your model. This is a common complaint. One solution is to subclass WebControl, but that's a mess.
You are already using the <%# Bind("...") %> syntax, so you have the right idea. That approach should work out-of-the-box with <asp:SqlDataSource>, but you want to update a custom class, so you need to use <asp:ObjectDataSource> instead. Use the approach in this article, except with ObjectDataSource instead of SqlDataSource.
But first you have to make your model (i.e., Class1) compatible with ObjectDataSource by marking it with [System.ComponentModel.DataObject] and designating the appropriate update method like this:
[System.ComponentModel.DataObjectMethodAttribute(
System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateProduct(string productName, ...) {
...
}
This would allow you to use an ObjectDataSource on your webform and finally get nice 2-way databinding. Read the links to for full details.
Visual Studio offers various clunky ways of automating this, such as TableAdapters and the infamous Strongly-Typed DataSet (STD), but those don't help people like yourself who have their own object model. I don't recommend STDs anyway.
I was thinking there was a better way?
I don't think your current approach is bad. If you're worried about having tons of logic in your webforms, you would gain much more by using an MVC approach than worrying about binding sugar...

Using DropDownList in FromView control

I'm trying to get a drop down list control to work in FormView. I need to have the list be a filtered view of a certain table and still be bound to a field in the data I'm editing. I've tried setting the item data programatically, ant that works but then the data binding
doesn't work, It tries to insert null into the database.
This is the code I've tried. I've also tried doing the same thing in several other events, it still tries to insert null into the database.
<asp:DropDownList ID="lstManagers" runat="server"
OnDataBound ="ManagersLoad"
SelectedValue='<%# Bind("UserName") %>' Width="100%"
DataSourceID="TimeOff" DataTextField="UserName" DataValueField="UserName">
</asp:DropDownList>
Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
'get list of managers
Using ef As New TimeOffData.TimeOffEntities
For Each item As ListItem In lst.Items
Dim li As ListItem = item
item.Text = (From x In ef.TimeOffUsers Where x.UserName = li.Value Select x.FirstName & " " & x.LastName).FirstOrDefault
Next
End Using
End Sub
I took all the data binding stuff of the control and just decide it would be easier to do it manually. I've changed the code to this,
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
e.Values.Item("ManagerName") = lst.SelectedValue
End Sub
Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
'get list of managers
Using ef As New TimeOffData.TimeOffEntities
Dim mng = From x In ef.TimeOffUsers Where x.IsManager = True
For Each item In mng
lst.Items.Add(New ListItem(item.FirstName & " " & item.LastName, item.UserName))
Next
End Using
End Sub

asp.net vb CheckBoxList Selected from CSV

I am learning asp.net and needed to have a CheckBoxList which items will be initially selected if the are in a CSV string from a database.
I have got it working although I just wondered if I have gone about it the best way as it seemed a little long winded?
Thanks for any help provided.
ASPX
<asp:CheckBoxList ID="rh_type" runat="server" CssClass="chkbox"
RepeatLayout="Flow" CausesValidation="True">
<asp:ListItem>House</asp:ListItem>
<asp:ListItem>Flat/Apartment</asp:ListItem>
<asp:ListItem>Bungalow</asp:ListItem>
<asp:ListItem>Any</asp:ListItem>
</asp:CheckBoxList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
Dim str_rh_type As String = "House,Bungalow"
Dim split As String() = str_rh_type.Split(","c)
For Each s As String In split
'Response.Write(s & "<br />")
For i = 0 To rh_type.Items.Count - 1
If rh_type.Items(i).Text = s Then
rh_type.Items(i).Selected = True
End If
Next
Next s
End Sub
Thanks again
J.
Your code is functional but maybe some tweaking for maintainability would help. Also not sure you necessarily need nested loops to load your drop down items.
This should be just a reference point to make your own decisions on coding practices. Certainly what works for some doesn't work for others.
Here's how I'd code this...
ASP.NET Control:
<asp:CheckBoxList ID="CheckBoxListHomeType" runat="server"
CssClass="chkbox" RepeatLayout="Flow" CausesValidation="True" />
...
ID of CheckBoxListHomeType is easy to remember and intellisense will get me the rest of the way. (or another common approach would be cblHomeType as the ID). Getting intellisense to help on a name like rh_type may be just as easy but IDs that resemble what kind of control it is can really help when maintaining code
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
LoadHomeTypes()
End If
End Sub
Protected Sub LoadHomeTypes()
Dim houseTypes = "House,Bungalow,Flat/Apartment,Any"
For Each houseType As String In houseTypes.Split(",")
CheckBoxListHomeType.Items.Add(New ListItem(houseType))
Next
End Sub
Keeping the logic in a separate LoadHomeTypes function can make the code more readable.
Creating a new ListItem while iterating the list of homeTypes should remove the need to iterate over the CheckBoxList items, (if you need to clear out the existing ones you can add CheckBoxListHomeType.Items.Clear() to the top of the function)
the Not Page.IsPostBack check prevents the need for loading the drop down values every postback, unless you have need for them to change.
This is the good answers , try this
Dim ds As DataSet
ds = Insertstu.searchrec(txtsearch.Text)
txtnm.Text = ds.Tables(0).Rows(0)("stuname").ToString()
txtadd.Text = ds.Tables(0).Rows(0)("stuaddress").ToString()
txtph.Text = ds.Tables(0).Rows(0)("stuph").ToString()
rdobtnsex.Text = ds.Tables(0).Rows(0)("sex").ToString()
Dim arr As String()
Dim quali As String = ds.Tables(0).Rows(0)("qualified").ToString()
arr = quali.Split(",")
Dim i As Integer
For Each itm As String In arr
For i = 0 To chkqualify.Items.Count - 1
If chkqualify.Items(i).Text = itm Then
chkqualify.Items(i).Selected = True
End If
Next
Next
''chkqualify is checkboxlist id

Put multiple data in one cell ASP.NET, VB.NET

How can i put multiple data in one datagrid cell? For example, a user can be a member of different organisations.
But i dont want the table to create a new datagrid row for the same user that is a member of multiple organisations.
This is how the code looks like right now:
Dim dSourceMemberOf As New SqlDataAdapter("QUERY", dbConn)
This fills the datagrid, but for some users there are more then 5 rows. So i'd like to put for one particular column all data in one cell.
How can this be done?
Edit:
This is how it woud look like right now:
username memberof
user1 dft
user1 kbo
user2 test
And this is how i want it to looke like:
username memberof
user1 dft
kbo
user2 test
I think the best solution is to group the data using a Linq query, then bind the grouped data to a gridview:
So the GridView would look like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="UserName" />
<asp:TemplateField>
<ItemTemplate>
<asp:ListBox ID="lst" runat="server" DataSource="<%# Container.DataItem.MemberOfGrouped %>" DataTextField="MemberOf"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and here is some example code to populate it:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim UngroupedData As List(Of YourData) = New List(Of YourData)()
Dim item1 As YourData = New YourData()
item1.MemberOf = "dft"
item1.UserName = "user1"
UngroupedData.Add(item1)
Dim item2 As YourData = New YourData()
item2.MemberOf = "kbo"
item2.UserName = "user1"
UngroupedData.Add(item2)
Dim grouped = From u In UngroupedData Group u By Key = u.UserName Into Group _
Select UserName = Key, MemberOfGrouped = Group.ToList()
GridView1.DataSource = grouped
GridView1.DataBind()
End Sub
Public Class YourData
Private _userName As String
Public Property UserName() As String
Get
Return _userName
End Get
Set(ByVal value As String)
_userName = value
End Set
End Property
Private _memberOf As String
Public Property MemberOf() As String
Get
Return _memberOf
End Get
Set(ByVal value As String)
_memberOf = value
End Set
End Property
End Class
Sorry, I think on paint was wrong, concept was right though - looks like you need to do it on the databind event event... more here (first three links will show you how to do what you require I think) - you could look at the row you are on, get the previous row, then decide what to do with a cell in your current row based on the prev row
You could use a table inside the GridView-Cell (or a Listbox like Ross Scott suggested).
<ItemTemplate>
<asp:table id="TblUserGroups" runat="server" Height="100%" Width="100%" CellSpacing="0" CellPadding="0">
</asp:table>
</ItemTemplate>
and in RowDataBound of gridview(ItemDataBound of Datagrid works similar):
Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim drUser As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
Dim TblUserGroups As Table = DirectCast(e.Row.FindControl("TblUserGroups"), Table)
Dim userID as int32 = Ctype(drUser(0), Int32)
Dim tblGroups as DataTable = getTblUserGroups(userID) 'write a function that returns the Groups of a given User f.e. as DataTable'
If tblGroups.Rows.Count = 0 Then
Dim tr As New TableRow
Dim td As New TableCell
Dim LblNoGroup As New Label
LblNoGroup .Text = "no user-group"
td.CssClass = "UserWithoutGroup"
td.Controls.Add(LblNoGroup)
tr.Cells.Add(td)
TblUserGroups.Rows.Add(tr)
Else
For Each groupRow As DataRow In tblGroups.Rows
Dim tr As New TableRow
Dim td As New TableCell
Dim LblGroup As New Label
LblGroup.Text = groupRow("GroupName").ToString 'The groups name column'
td.Controls.Add(LblGroup)
tr.Cells.Add(td)
TblUserGroups.Rows.Add(tr)
Next
End If
End Select
End Sub
I think this is the most flexible way.

Resources