Is there DataBound in DataGrid? - asp.net

in GridView:
proctected sub gridview.DataBound(Byval sender as object, byval e assystemEventArgs) handles gridview.databound
{
}
how to use DataBound in DATAGRID??

You can try with this code based on OnItemDataBound
<asp:DataGrid
id="ItemsGrid"
runat="server"
OnItemDataBound="Item_Bound"
.../>
//Code behind
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
Label1.Text = Label1.Text + " " + e.Item.ItemIndex;
}

To be honest, your question doesn't make much sense currently. But if you want to handle the GridView's DataBound event (as opposed to the RowDataBound event):
Protected Sub Gridview1_DataBound(sender As Object, e As System.EventArgs) Handles Gridview1.DataBound
Dim grid = DirectCast(sender, GridView)
Dim dataSource As Object = grid.DataSource
For Each row As GridViewRow In grid.Rows
' do something, looping all rows in the grid with RowType=DataRow '
Next row
End Sub

Related

setting radiobuttonlist value within a gridview using code behind

I am trying to set the selected value of a radiobuttonlist that is a template column within a gridview. I am having to do this using the code behind as the database field containing the data (Status) contains null values hence I cant used SelectedValue='<%# Bind("Status") %>' on the asp side.
It has been suggested that I use onRowDataBound to do this and use DataItem to retrieve the value from the datasource and used it to set the radiobuttonlist selected value but I'm not sure how to do this in vb code.
I've tried the following:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)
' this will be the object that you are binding to the grid
Dim myObject = TryCast(e.Row.DataItem, DataRowView)
Dim sStatus As String = Convert.ToString(myObject("Status"))
If sStatus <> Nothing Then
radioList.SelectedValue = sStatus
End If
End If
End Sub
but it hasnt work. Any help would be appreciated. Thanks
Found my own solution as follows:
Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)
Dim selected As String = e.Row.DataItem("Status").ToString
If Not IsNothing(selected) Then
rbl.SelectedValue = selected
End If
End If
End Sub

How to switch back to EditTemplate after an Update to ItemTemplate in a ListView

It have a ListView with LayoutTemplate, ItemTemplate and EditTemplate. The List view only ever shows one item.
The ItemTemplate as an EditButton with CommandName = 'Edit' which when clicked triggers...
Protected Sub ListView1_ItemEditing(ByVal sender As Object, ByVal e As ListViewEditEventArgs) Handles ListView1.ItemEditing
ListView1.EditIndex = e.NewEditIndex
'Create SQL and load result in datatable and bind to listview
LoadData(Session("SID"))
End Sub
The EditTemplate has an UpdateButton with CommandName 'Update' which when clicked triggers...
Protected Sub ListView1_Command(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles ListView1.ItemCommand
If e.CommandName = "Update" Then
'Use StringBuilder to build up an UPDATE TSql script
SqlStr = sb.ToString
'Execute Update
ExecuteSQLScript(SqlStr)
End If
The above works great. My question is how do I now get back to ItemTemplate View. I know that I must use the ItemUpdating method similar to the way the above ItemEditing method worked, but I've ran out of ideas... Any help appreciated. All I have at the moment is...
Protected Sub ListView1_Updating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs) Handles ListView1.ItemUpdating
'ListView1.ItemIndex = e.ItemIndex 'This does not work
LoadData(Session("SID"))
End Sub
Thanks
Set the EditIndex to -1.
ListView1.EditIndex = -1

an error when add blank data on top drop downlist

I have a dropdown, I have a datasource and i wanted to add blank data it worked but the value that selected always 0
My code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
listhostel()
End Sub
Protected Sub listhostel()
Dim dash As New DataSet
Dim clas As New Class1
dash = clas.returndataset("select ID as idhostel,Nama from Hostel")
listhotelx.DataSource = dash
listhotelx.DataBind()
listhotelx.Items.Insert(0, New ListItem("-- Select --", 0))
End Sub
Protected Sub viewdatagrid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles viewdatagrid.Click
Label1.Text = listhotelx.SelectedValue
viewdata()
End Sub
The file.aspx
<asp:DropDownList ID="listhotelx" runat="server" DataTextField="Nama"
DataValueField="idhostel" >
</asp:DropDownList>
After you databind and add the item, notice how you can also set the items SelectedIndex:
listhotelx.Items.Insert(0, new ListItem("-- Select --", String.Empty));
listhotelx.SelectedIndex = 0;
Error you getting because ListItem must have (string,string) as an argument and you are passing ("-- Select --",0) which is (string,int).
If you want to add blank data on top then you just need to enter empty string.
listhotelx.Items.Insert(0, new ListItem(String.Empty, String.Empty));
listhotelx.SelectedIndex = 0;
on Page_Load must add Page.IsPostBack
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
listhostel()
End If
End Sub
Call the DropDownList.DataBind() method after adding the new item to dropdownlist.
The ListItem method accept only string values, so in your code replace 0 with "0"
Protected Sub listhostel()
Dim dash As New DataSet
Dim clas As New Class1
dash = clas.returndataset("select ID as idhostel,Nama from Hostel")
listhotelx.DataSource = dash
listhotelx.Items.Insert(0, New ListItem("-- Select --", "0")) // here the 2nd argument is string.
listhotelx.DataBind()
End Sub
// I have checked this code and it works fine for me
I have also add an item to my dropdownlist
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="name1" Value="-1" ></asp:ListItem>
</asp:DropDownList>
And on the button click when I add a new item, It successfully adds the item at index 0.
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0,new ListItem("name", "-1"));
DropDownList1.DataBind();
}

listbox selected items into textbox not working

my vb.net will not use listbox1.selecteditems it always comes up with a blue line underneath even though when i search online everyone is using this.
my goal is to get the selected items and list them in a textbox
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim li As ListItem
For Each li In ListBox1.Items
If li.Selected Then
TextBox1.Text &= li.Text & vbCrLf
End If
UpdatePanel2.Update()
Next
End Sub
End Class
To determine the selection in a multi-selection list control
Loop through the control's Items collection and test the Selected property of every individual item.
For Each li In ListBox1.Items
If li.Selected Then
TextBox1.Text &= li.Text & vbCrLf
End If
Next
MSDN: To determine the selection in a multi-selection list control
SelectedItems is not available in ASP.Net, this property exists only for Winforms-Listbox Controls.
I think the problem here is that you're binding to the control immediately prior to trying to retrieve a selected value from it. When the control is initially bound to the datasource it won't have any selected items.
You need to split it out so that you bind the listbox when the page is loaded, then the user selects some things in that box, clicks your Button2, and the value of the textbox is updated on postback.
First, on page load:
Protected Sub Page_Load(object sender, EventArgs, e)
listcmd.Connection = conn1
conn1.Open()
listcmd.CommandText = "SELECT distinct B603SalesAsOFMASTER.SDESCR FROM B603SalesAsOFMASTER"
listda.Fill(saolist, "listboxtext")
Dim dt As DataTable = saolist.Tables("listboxtext")
ListBox1.DataSource = dt
ListBox1.DataValueField = "SDESCR"
ListBox1.DataMember = "SDESCR"
ListBox1.DataBind()
conn1.Close()
End Sub
Then, this code is run when the user clicks Button2
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
TextBox1.Text &= DirectCast(ListBox1.SelectedItems(i), DataRowView)(1).ToString & vbCrLf
Next
CheckBox1.Visible = True
TextBox1.Visible = True
End Sub
To view (Text Item in textbox)
textbox3.Text = listBox1.GetItemText(listBox1.SelectedItem);

need to alter a dropdownlist after it's been databound to an sql data source

Ok so I've got my Drop down list databound to an sql data source, now I need to change the data in a few of the fields before it gets displayed. I've been messing with this all morning and I can't find anything useful, this is what I've got so far and it's clearly not working...
Protected Sub ddlBookType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBookType.DataBound
'ddlBookType.Items.Insert(0, "Any")
Dim i As Integer
For i = 0 To ddlBookType.Items.Count - 1
If ddlBookType.Items(i).Attributes.Equals("mod_cook") Then
ddlBookType.Items(i).Text.Replace("mod_cook", "Modern Cooking")
End If
Next
End Sub
Hey I've just done it in C# and converted it into VB (I hope it's right).
Here's the code (
Protected Sub ddlBookType_DataBound(sender As Object, e As EventArgs)
Dim ddlBookType As DropDownList = DirectCast(sender, DropDownList)
For Each item As ListItem In ddlBookType.Items
If item.Text = "mod_cook" Then
item.Text = "Modern Cooking"
End If
Next
End Sub
And here's the original C# code
protected void ddlBookType_DataBound(object sender, EventArgs e)
{
DropDownList ddlBookType = (DropDownList)sender;
foreach (ListItem item in ddlBookType.Items)
{
if (item.Text == "mod_cook")
{
item.Text = "Modern Cooking";
}
}
}

Resources