check radiobutton according to the value of row item in dataset - vb.net

I want to check radiobutton which is depends on value of specific row item in the dataset.
Dim adp As SqlDataAdapter = New SqlDataAdapter _
("select top 1 * from tbl_party_record order by rid desc", con)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
View.TextBox1.Text = ds.Tables(0).Rows(0).Item(1)
View.TextBox2.Text = ds.Tables(0).Rows(0).Item(2)
please consider item3 here as ds.Tables(0).Rows(0).Item(3).
If value of item3 is male then select radio button male elseif value of item3 is female then select radio button female elseif value of item3 is other then select radio button other.
how can I achieve this?
Thanks guys...

The logic is quite straightforward, I think. To set a radio button in ASP.NET you can use the Checked property:
'First un-check them all.
radiomale.Checked = False
radiofemale.Checked = False
radioother.Checked = False
'Now set the right one checked based on the data
Dim genderValue As String = ds.Tables(0).Rows(0).Item(2)
Select Case genderValue
Case "male"
radiomale.Checked = True
Case "female"
radiofemale.Checked = True
Case "other"
radioother.Checked = True
End Select
If you haven't already, you should also make sure all your radio buttons have the same value for the GroupName property, so that the user cannot select more than one radio button at the same time, and also so the value is posted back to the server properly on the next submit.

Related

Unbound ComboBox populates another combobox

How do I get a combobox to populate depending on what value you chose in another combobox? The first combobox values come from a table. The second combobox could either be a list or a date. Working on list part right now. If the value will give a list, the unbound combobox is visible and enabled. That works - I just can't get the second combobox to populate - should be values from another "code" table. Tried when exiting the first combobox OR when entering the second combobox. Neither works (used same code). Help!
Private Sub Combo746_Exit(Cancel As Integer)
Dim strsql As String
If (Me!Combo746.Value = "Category") Then
Me.Combo750.Enabled = True
Me.Combo750.visible = True
Me.Text748.Enabled = False
Me.Text748.visible = False
Me.Combo750.SetFocus
strsql = "Select [Category] FROM [dbo_CodesCategory]"
Me!Combo750.RowSource = strsql
Else
End If
End Sub
I got this to work. First combobox does not populate second combobox, but instead goes to the correct combobox with values. To explain - Department values are in the Department combobox - combodept. Category values are in a Category combobox - combocat. First combobox has values Category and Department. If the user picks Department, combodept for Department will be visible, combobox for Category- combocat - will be invisible. Here is the code:
If (Me!Combo746.Value = "Category") Then`
Me.ComboCat.Enabled = True
Me.ComboCat.visible = True
Me.ComboDept.Enabled = False
Me.ComboDept.visible = False
Me.ComboCat.SetFocus
Else
If (Me!Combo746.Value = "Department") Then
Me.ComboDept.Enabled = True
Me.ComboDept.visible = True
Me.ComboCat.Enabled = False
Me.ComboCat.visible = False
Me.ComboDept.SetFocus
End If
End If

Listview help needed

I am confused being a relatively new user to vb.net. Why my listview is not amending the value in the list? If I may, so I have a correct working of how listview displays its data from database. I have a general question in addition to my code problem.
I have five columns in my listview (0-4). Am I correct in saying that if my access database contained say 10 fields but I only needed to display five of them but one of them was field (9), then would code the list like my code below, which is not changing the value and will only display the list if I remove the 'else' statement.
What is the error? Many thanks
UPDATED CODE:
oledbCnn.ConnectionString = My.Settings.storageConnectionString
oledbCnn.Open()
'drcount = Convert.ToInt32(dr("RowCount"))
sql = "Select TOP 100 * from Requests ORDER BY [Date-time received] DESC"
Debug.Print(sql)
Dim oledbCmd As OleDbCommand = New OleDbCommand(sql, oledbCnn)
Using dr = oledbCmd.ExecuteReader()
'clear items in the list before populating with new values
'lvRequests.Items.Clear()
While dr.Read()
If dr.HasRows Then
Dim LVI As New ListViewItem
With LVI
.Text = dr(0).ToString()
.UseItemStyleForSubItems = False
.SubItems.Add(CDate(dr(5)).ToShortDateString())
.SubItems.Add(dr(1).ToString())
.SubItems.Add(dr(3).ToString())
If dr(3).ToString = "D" Then
.SubItems(3).Text = "Destroyed"
ElseIf dr(3).ToString = "O" Then
.SubItems(3).Text = "Out"
ElseIf dr(3).ToString = "I" Then
.SubItems(3).Text = "Intake"
End If
.SubItems.Add(dr(9).ToString())
If dr(9).ToString = "DEMO" Then
.SubItems(9).Text = "Done"
End If
End With
lvRequests.Items.Add(LVI)
lvcount += 1
End If
End While
End Using
There is not enough info to be really sure what is going on. It appears that you pull 10 columns from the DB, but if you only post 5 of them to the LV, then there should only be 5 subitems. So this is wrong:
If dr(9).ToString() = "DEMO" Then
lvRequests.Items(lvRequests.Items.Count - 1).SubItems(9).Text = "Done"
' should probably be:
If dr(9).ToString() = "DEMO" Then
lvRequests.Items(lvRequests.Items.Count - 1).SubItems(4).Text = "Done"
What might make it clearer at least in code would be to use Names for the subitems. If you instance a SubItem object rather than use a default constructor, you can assign a name and reference them that way instead:
Dim si As New ListViewItem.ListViewSubItem
With si
.Text = dr(X).ToString ' dunno whats in there
' this is probably not exactly right, but give the SubItem
' the same name as the db column
.Name = dr.Table.Columns(X).ColumnName
End With
thisItem.SubItems.Add(si) ' add sub to Item collection
Now, your code can use the name rather than index:
If dr(9).ToString() = "DEMO" Then
lvReq.Items(lvReq.Items.Count - 1).SubItems("TheColumnName").Text = "Done"
it no longer really matters how many columns or subitems there are. The ListViewItem.SubItems collection does not require sub item names to be unique, so it is possible to end up with 2 with the same name, but if you map from the DR/DT/DB correctly, it should take care of itself.
If the LV is bound to the datasource (you did not say), then there could be as many SubItems as db/datasource columns (never actually used an LV that way).

new records to add first row of datagrid view in vb.net

In my VB.Net application I am filling my data grid view like this:
Dim cmdd1 As New SqlCommand("DashBordFetch1", con.connect)
cmdd1.CommandType = CommandType.StoredProcedure
cmdd1.Parameters.Add("#locid", SqlDbType.Int).Value = locid
da1.SelectCommand = cmdd1 dr = cmdd1.ExecuteReader
While dr.Read
flag = False
carid1 = dr("Car_Id")
For j = 0 To dcnt1 - 2
If carid1 = dgv.Item(0, j).Value Then
flag = True
Exit For
End If
Next
If flag = False Then
If dr("Car_Id") Is DBNull.Value Then
carid1 = "null"
Else
carid1 = dr("Car_Id")
End If
If dr("Plate_Source") Is DBNull.Value Then
platesource1 = "Null"
Else
platesource1 = dr("Plate_Source")
End If
Dim row1 As String() = {carid1, platesource1}
DGVDeliverd.Rows.Add(row1)
End If
End While
..also i am using Timer..every 2 minutes timer will work .some time new records will added to my datagridview..while adding new record that is adding to last row of my datagrid view,,i want to add every time new records to my first row of datagridview. how i can do this?
I am using windows forms
You can use Insert instead of Add to specify the position for the new row. So always insert at position zero:
DGVDeliverd.Rows.Insert(0, row1)
See MSDN here for details (screenshot below).
Use:
Order by your primary id desc
to keep the latest data on top of the grid
update #1
DGVDeliverd.Controls[0].Controls.AddAt(0, row1);

vb2008 retrieve value from combobox selected item and display to multiple textboxes

I wanted to ask how to get combobox's selected value then display it to multiple textboxes. The combobox is populated with illnesses from the database. Now when an illness is selected from combobox, the symptoms of that illness should be displayed in many textboxes. Currently I have 10 textboxes for the symptoms. The table structure is id, illness, symptoms. Here's my code:
Dim mycmd As New MySqlCommand
Dim dtr As MySqlDataReader
Call Connect()
Dim str As String
str = "Select symptoms from diagnose where illness = #ill"
mycmd.Parameters.AddWithValue("ill", cmbRecord.Text)
mycmd.CommandText = str
dtr = mycmd.ExecuteReader
While dtr.Read()
symp0.Text = dtr("symptoms")
symp1.Text = dtr("symptoms")
symp2.Text = dtr("symptoms")
symp3.Text = dtr("symptoms")
symp4.Text = dtr("symptoms")
symp5.Text = dtr("symptoms")
symp6.Text = dtr("symptoms")
symp7.Text = dtr("symptoms")
symp8.Text = dtr("symptoms")
symp9.Text = dtr("symptoms")
End While
myConn.Close()
when an illness is selected from the combobox the symptoms should display on those textboxes. Say the selected illness has only 4 symptoms in the table, then symp0 to symp3 textbox will show the symptoms one by one and leaving the remaining textboxes blank.
The problem is that when an illness is selected, those textboxes displays only the last symptom of that illness stored in the database.
Example: fever. In the table, it has 4 symptoms: cold, hot temperature, headache, dizziness. If fever is selected, only dizziness is displayed from symp0 to symp9 textboxes.
dtr("symptoms") gets a value of column "symptoms" from the current row. Between assignment to Textboxes #1 through #10 you do not advance reader to the next row, this is why you are seeing the same value in all textboxes.
Now to answer your question why it's the last symptom in all textboxes. By looping through rows with dtr.Read(), you re-assign values of Textboxes every time, and it happens until the last one is assigned. At which point no subsequent assignments are made, so the last value remains on the screen.
What you are looking for is probably something like this:
Dim symptoms As New List(Of String)
While dtr.Read()
symptoms.Add(dtr("symptoms"))
End While
'set available symptoms
Dim arrayOfTextboxes() As TextBox = {symp0, ... put all textboxes here..., symp9}
Dim i As Integer = 0
For i = 0 To symptoms.Count - 1
arrayOfTextboxes(i).Text = symptoms(i)
Next
'clear other textboxes
For j = i to UBound(arrayOfTextboxes)
arrayOfTextboxes(j).Text = String.Empty
Next
This is assuming you have less than 10 symptoms to display. For any number of symptoms, you could have used a ListBox by setting its DataSource to symptoms. Your code becomes more compact:
Dim symptoms As New List(Of String)
While dtr.Read()
symptoms.Add(dtr("symptoms"))
End While
YourListBoxWithSymptoms.DataSource = symptoms

DataGridView column order does not seem to work

I have a DataGridView bound to a list of business objects:
Dim template As New IncidentTemplate
Dim temps As List(Of IncidentTemplate) = template.LoadAll
Dim bs As New BindingSource
If Not temps Is Nothing Then
bs.DataSource = temps
Me.dgvTemplates.DataSource = bs
End If
I then add an unbound button column and make some of the bound columns invisible:
Dim BtnCol As New DataGridViewButtonColumn
With BtnCol
.Name = "Edit"
.Text = "Edit"
.HeaderText = String.Empty
.ToolTipText = "Edit this Template"
.UseColumnTextForButtonValue = True
End With
.Columns.Add(BtnCol)
BtnCol = Nothing
For Each col As DataGridViewColumn In Me.dgvTemplates.Columns
Select Case col.Name
Case "Name"
col.DisplayIndex = 0
col.FillWeight = 100
col.Visible = True
Case "Description"
col.DisplayIndex = 1
col.FillWeight = 100
col.Visible = True
Case "Created"
col.DisplayIndex = 3
col.HeaderText = "Created On"
col.DefaultCellStyle.Format = "d"
col.FillWeight = 75
col.Visible = True
Case "CreatedBy"
col.DisplayIndex = 2
col.HeaderText = "Created By"
col.FillWeight = 75
col.Visible = True
Case "Edit"
col.DisplayIndex = 4
col.HeaderText = ""
col.FillWeight = 30
col.Visible = True
Case Else
col.Visible = False
End Select
Next
This seems to work reasonably well except no matter what I do the button column (Edit) always displays in the middle of the other columns instead of at the end. I've tried both DGV.Columns.Add and DGV.Columns.Insert as well as setting the DisplayIndex of the column (this works for all other columns) but I am unable to make the button column display in the correct location. I've also tried adding the button column both before and after setting the rest of the columns but this seems to make no difference Can anyone suggest what I am doing wrong? It's driving me crazy....
I stumbled on your post while looking to solve a similar problem. I finally tracked it down by doing as you mention (using the DisplayIndex property) and setting the AutoGenerateColumns property of the DataGridView to false. This property is not visible in the designer, so I just added it to the constructor for my form. Be sure to do this before setting the data source for your grid.
Hope this helps...
The AutoCreateColumn is the problem. The following article gives an example for how to fix it.
From DataDridView DisplayOrder Not Working
When setting the column’s DisplayIndex in a data grid view, some columns were out of order. To fix the issue, I had to set AutoGenerateColumns to true before setting the data source, and to FALSE after.
For Example:
dgvReservedStalls.AutoGenerateColumns = True
dgvReservedStalls.DataSource = clsReservedStalls.LoadDataTable()
dgvReservedStalls.AutoGenerateColumns = False
Same problem here, and after searching for a while, I found out that by setting the DisplayIndexes in Ascending order made it for me.
It's counter-intuitive, because it's a number, but I still had to set them in order.
This works:
With customersDataGridView
.Columns("ContactName").DisplayIndex = 0
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
.Columns("Country").DisplayIndex = 3
.Columns("CompanyName").DisplayIndex = 4
End With
While this did not:
With customersDataGridView
.Columns("ContactName").DisplayIndex = 0
.Columns("CompanyName").DisplayIndex = 4
.Columns("Country").DisplayIndex = 3
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
End With
I tried the above solutions without success. Then I found this article: It made it all better.
http://msdn.microsoft.com/en-us/library/vstudio/wkfe535h(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1