Bind Value From XMLWebService to Dropdownlist in ASP.Net - asp.net

How to bind data from an XMLWebService to a DropDownList?
I have this code but it doesn't work:
Dim xMaster As New MasterService.Masterservice
DT = xMaster.GetDataProgram
DT.Load(DT)
DDL.DataSource = DT
DDL.DataTextField = DT.Columns("cdProgram").ColumnName.ToString()
DDL.DataValueField = DT.Columns("nmProgram").ColumnName.ToString()
DDL.DataBind()
I tried to bind it to the GridView and the values are shown. But, when I tried to bind it to the dropdownlist, it returns nothing. Thank you very much.

Try this:
DDL.DataTextField = "cdProgram"
DDL.DataValueField = "nmProgram"

Related

Add new Column to existing Dataview

I wanted to add New Column to the Existing Dataview as shown in below image. the new column should have value 1 or 0 base upon the value of DocumentType,HouseOrigin and StorageDate. basically i am checking if Document type and HouseOrigin is same then put 0 for the latest storageDate and put 1 for other row.
Hope below image will help to understand.
How i will do it ? TIA.
this is how i am get it. Hope that helps !!
If DetailsView.Count > 0 Then
Dim col As DataColumn = New DataColumn("DisableFlag", GetType(Boolean))
col.DefaultValue = True
DetailsView.Table.Columns.Add(New DataColumn("DisableFlag", GetType(Boolean)))
Dim LocalDV As DataView = DetailsView
Dim result As List(Of DataTable) = DetailsView.Table.AsEnumerable.GroupBy(Function(row) row.Field(Of String)("DocumentType")).[Select](Function(g) g.CopyToDataTable()).ToList()
For Each rowView As DataRowView In DetailsView
Dim row As DataRow = rowView.Row
For Each DivByDocTypedt As DataTable In result
Dim maxDate As Object = DivByDocTypedt.Compute("MAX(StorageDate)", Nothing)
For Each DivByDocTyperow As DataRow In DivByDocTypedt.Rows
If (DivByDocTyperow.Item("Guid") = row.Item("Guid")) Then
If (maxDate = row.Item("StorageDate")) Then
row.SetField("DisableFlag", False)
Else
row.SetField("DisableFlag", True)
End If
End If
Next '' DivByDocTyperow
Next ''DivByDocTypedt
Next ''rowView of DetailsView

How to select some data from DataTable

I want to search some data from DataTable to show in GridView.
Like
(select * from customer where id="1")
Can I do that?
DataRow[] foundRows = yourTable.Select("id=1");
Or you can filter rows in default view:
yourTable.DefaultView.RowFilter = "id = 1";
GridView gv = new GridView();
gv.DataSourse = yourTable.DefaultView
gv.DataBind()

gridview PageIndexChanging issue

I am trying to to loop through a dataset's value through the rows in a gridview and color in the text if that row matches.
The code below works however whenever I change the page through the PageIndexChanging and this function is ran again, the coloring doesn't work anymore. It still loops through the gridview if there is a match but the effects are not shown.
--variable initialization class instantiation--
--code to connect to db here--
mySQLCommand.CommandText = "SELECT ..."
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDataset)
Me.MainPageGridView.DataSource = myDataset
Me.MainPageGridView.DataBind()
mySQLCommand.CommandText = "SELECT ... The ID's to be matched"
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDatasetNew)
Me.MainPageGridView.DataSource = myDatasetNew
For Each dataRow In myDataset.Tables(0).Rows
thisID = dataRow("ID").ToString
For Each gvRow In Me.MainPageGridView.Rows
If gvRow.Cells(2).Text = thisID Then
For column = 0 To 14 Step 1
gvRow.Cells(column).ForeColor = Drawing.Color.RosyBrown
Next
Exit For
End If
Next
Next
Why don't you use MainPageGridView_RowDataBound event to match the id? I have re-factored your original code to something like below, please check and let me know if it works:
'In DataBind or some other method
'Load(myDataSet)
mySQLCommand.CommandText = "SELECT ..."
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDataset)
'Load myDatasetNew and bind it to grid
mySQLCommand.CommandText = "SELECT ... The ID's to be matched"
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDatasetNew)
Me.MainPageGridView.DataSource = myDatasetNew
Me.MainPageGridView.DataBind()
and perform id matching in
Protected Sub MainPageGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MainPageGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As String = DataBinder.Eval(e.Row.DataItem, "ID") 'The name of ID column in "myDatasetNew"
Dim dv As System.Data.DataView = myDataset.Tables(0).DefaultView
dv.RowFilter = "ID = " & id
If dv.Count > 0 Then 'id matches
'Change foreclor of entire row
e.Row.ForeColor = Drawing.Color.RosyBrown
End If
End If
End Sub
You really need to do your data comparison in the GridView.RowDataBound event.

sort dropdownlist by date, after it is populated from datasource

I want to sort the dropdownlist by date, but i cant figure out how.
ddate.DataSource = myTable
ddate.DataTextField = "ddate7"
ddate.DataValueField = "ddate7"
ddate.DataBind()
If myTable is a DataTable then you could put it into a Dataview and sort it there like this:
Dim dv As New DataView(myTable)
dv.Sort = "ddate7"
ddate.DataSource = dv
ddate.DataTextField = "ddate7"
ddate.DataValueField = "ddate7"
ddate.DataBind()
You can use a DataView to sort and filter the DataTable you may try the following code,
DataView dv = new DataView(myTable);
dv.Sort = "ddate7 ASC";
ddate.DataSource = dv;
ddate.DataTextField = "ddate7";
ddate.DataValueField = "ddate7";
ddate.DataBind();
Good luck.

How to correctly filter a datatable (datatable.select)

Dim dt As New DataTable
Dim da As New SqlDataAdapter(s, c)
c.Open()
If Not IsNothing(da) Then
da.Fill(dt)
dt.Select("GroupingID = 0")
End If
GridView1.DataSource = dt
GridView1.DataBind()
c.Close()
When I call da.fill I am inserting all records from my query. I was then hoping to filter them to display only those where the GroupingID is equal to 0. When I run the above code. I am presented with all the data, the filter did not work. Please can you tell me how to get this working correctly. Thanks.
dt.Select() returns an array of DataRows.
Why don't you use a DataView?
DataView dv = new DataView(dt);
dv.RowFilter = "GroupingID = 0";
GridView1.DataSource = dv;
Junt in case... Think you've got a small typo in your VB.NET code. It should be dv.RowFilter instead of dv.RowStateFilter, so:
Dim dt As New DataTable
Dim dv As New DataView(dt)
dv.RowFilter = "GroupingID = 0"
DataGridView1.DataSource = dv
The accepted answer is correct, though it should have been given in vb.net to better benefit the one that asked the question. Here it is in VB.Net:
Dim dt As New DataTable:Dim dv As New DataView(dt):dv.RowStateFilter = "GroupingID = 0":DataGridView1.DataSource = dv

Resources