如何使用radiobutton和checklistbox保存值以访问数据库? [英] How do you save values with radiobutton and checklistbox to access database?

查看:61
本文介绍了如何使用radiobutton和checklistbox保存值以访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在radiobutton和checklistbox中放置值,以便在我选择它时,它将保存在我的数据库中?

How can I put values in radiobutton and checklistbox so that when I select it, it will be saved in my databse?

我正在使用此代码绑定数据:

I am using this code to bind data:

 试试¥b $ b            Me.StudentsTableAdapter.FillBy(Me.ComputerDataSet.Students)

        Catch ex As System.Exception

            MsgBox("保存时出错")

       结束尝试

  Try
            Me.StudentsTableAdapter.FillBy(Me.ComputerDataSet.Students)
        Catch ex As System.Exception
            MsgBox("Error in saving")
        End Try




推荐答案

你好,

注意首先关闭以下内容是不能设置使用TableAdapters,他们个人会限制你可以做什么,更少自由做我喜欢的事情,如下所示如果你需要更改底层的
数据库模式,那么你可能不想再去了,除非重新考虑离开TableAdapter。

Note First off the following is not setup to work with TableAdapters, personally they will constrain what you can do, less freedom to do things like what I present below and are prone to be troublesome if you need to change the underlying database schema so you may not want to go any farther unless reconsidering moving away from TableAdapters.

使用单选按钮的一个选项是麻烦的是动态创建并将它们放入面板中。当您需要知道所选的单选按钮时,可以使用LINQ语句来获取它们。当然,困难的部分是在下一个下面创建它们并使
确保它们适合或设置面板滚动。

One option for working with Radio buttons is to create and place them into a panel dynamically. When you need to know the selected Radio button you can use a LINQ statement to get them. Of course the hard part is to create them one below the next and make sure they fit or setup the panel to scroll.

在面板中获取所选单选按钮的示例我们将Tag属性设置为加载记录的主键。这里我们有一个带有几个RadioButton控件的面板。

Example to get the selected radio button in a panel where we had set the Tag property to the primary key of the record loaded. Here we have a panel with several RadioButton controls.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim selected = Panel1.Controls.OfType(Of RadioButton).Where(Function(rb) rb.Checked).FirstOrDefault
    If selected IsNot Nothing Then
        MessageBox.Show(


" id {CInt (selected.Tag)} - {selected.Text}")
Else
MessageBox.Show(" None selected")
End if
End Sub
"id {CInt(selected.Tag)} - {selected.Text}") Else MessageBox.Show("None selected") End If End Sub

或者创建一个DataGridView来模仿一堆RaidioButton控件。

Or create a DataGridView to mimic a bunch of RaidioButton controls.

上面有很多代码

Public Class Form1
    WithEvents bsAnswers As New BindingSource

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ops As New DataOperations

        bsAnswers.DataSource = ops.ReadMockedData
        DataGridView1.DataSource = bsAnswers
        DataGridView1.Columns("SelectionColumn").Width = 25
        DataGridView1.Columns("SelectionColumn").HeaderText = ""
        DataGridView1.Columns("OptionName").HeaderText = ""
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

        Dim Value As Boolean = CBool(DataGridView1.Rows(e.RowIndex).Cells("SelectionColumn").Value)
        If Value Then
            Dim Index As Integer = e.RowIndex
            For row As Integer = 0 To DataGridView1.Rows.Count - 1
                If row <> Index Then
                    DataGridView1.Rows(row).Cells("SelectionColumn").Value = False
                End If
            Next
        Else
            DataGridView1.Rows(e.RowIndex).Cells("SelectionColumn").Value = True
            Dim Index As Integer = e.RowIndex
            For row As Integer = 0 To DataGridView1.Rows.Count - 1
                If row <> Index Then
                    DataGridView1.Rows(row).Cells("SelectionColumn").Value = False
                End If
            Next
        End If

        ' Force cell painting
        DataGridView1.CurrentCell = DataGridView1(0, e.RowIndex)

    End Sub
    Private Sub DataGridView1SelectAll_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
        If TypeOf DataGridView1.CurrentCell Is DataGridViewCheckBoxCell Then
            DataGridView1.EndEdit()
        End If
    End Sub
    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        If e.ColumnIndex = DataGridView1.Columns("SelectionColumn").Index AndAlso e.RowIndex >= 0 Then
            e.PaintBackground(e.ClipBounds, True)

            Dim rectButton As Rectangle

            rectButton.Width = 14
            rectButton.Height = 14
            rectButton.X = e.CellBounds.X + (e.CellBounds.Width - rectButton.Width) \ 2
            rectButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectButton.Height) \ 2

            If IsDBNull(e.Value) OrElse CBool(e.Value) = False Then
                ControlPaint.DrawRadioButton(e.Graphics, rectButton, ButtonState.Normal)
            Else
                ControlPaint.DrawRadioButton(e.Graphics, rectButton, ButtonState.Checked)
            End If

            e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus)
            e.Handled = True
        End If
    End Sub
    Private Sub cmdSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelected.Click

        Dim Selection =
            (
                From T In CType(bsAnswers.DataSource, DataTable).AsEnumerable
                Where T.Field(Of Boolean)("SelectionColumn")
                Select New Selection With
                    {
                        .id = T.Field(Of Integer)("Id"),
                        .OptionValue = T.Field(Of String)("OptionName"),
                        .MiscInfo = T.Field(Of String)("Misc")
                    }
            ).FirstOrDefault

        If Selection IsNot Nothing Then
            '
            ' We have the selected item which includes it's primary key
            '
            MessageBox.Show(


" Canidate:{Selection.OptionValue} {Environment.NewLine} Id:{Environment.NewLine& Selection.id}")
Else
MessageBox.Show(" No selection made")
End if
End Sub

Private Sub Button1_Click (发送者为对象,e为EventArgs)处理Button1.Click
Dim selected = Panel1.Controls.OfType(Of RadioButton).Where(Function(rb)rb.Checked).FirstOrDefault
如果选择IsNot Nothing然后
MessageBox.Show(
"Canidate: {Selection.OptionValue}{Environment.NewLine}Id:{Environment.NewLine & Selection.id}") Else MessageBox.Show("No selection made") End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim selected = Panel1.Controls.OfType(Of RadioButton).Where(Function(rb) rb.Checked).FirstOrDefault If selected IsNot Nothing Then MessageBox.Show(


这篇关于如何使用radiobutton和checklistbox保存值以访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆