问题描述
我有带有复选框的datagridview,复选框表示当管理员检查全部或部分复选框并点击提交按钮时员工的注意力。根据Datagridview中选中的复选框将当前和不存在的值插入数据库表中。
如何做到这一点尝试这个但是失败了
帮助我...
这个函数在form_load事件中调用,用datagridview绑定表,我用动态创建的checkBox
i have datagridview with checkbox, checkbox represent as attendence of employee when admin check all or some checkboxes and click on submit button. according to selected checkbox in Datagridview values of present and absent is inserted in database table.
how to do this m try this but m getting failed
help me...
this function is called at form_load event to bind tables with datagridview ,with checkBox i have created at dynamic
Private Sub LoadRecord()
cmdselect = "select ID,Name,Gender,Position from tblcouriercompany"
da = New SqlDataAdapter(cmdselect, con)
dt = New DataTable()
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
[]
表格看起来像这样请帮助我编码m不理解如何链接复选框与数据库列
https://www.dropbox.com/s/r7fwhlllrkerlli/Untitled.png?m=[^]
form looks like this please help me in coding m not understanding how to link checkbox with database column
推荐答案
USE [MyDatabase]
GO
/****** Object: Table [dbo].[mst_employees] Script Date: 03/02/2014 19:11:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[mst_employees](
[Emp_ID] [int] NULL,
[Name] [varchar](50) NULL,
[Gender] [char](10) NULL,
[Position] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
2. emp_attendance(Sql脚本在这里)
2. emp_attendance (Sql script is here)
USE [MyDatabase]
GO
/****** Object: Table [dbo].[emp_attendance] Script Date: 03/02/2014 19:12:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[emp_attendance](
[Att_ID] [bigint] IDENTITY(1,1) NOT NULL,
[Emp_ID] [int] NULL,
[Att_Date] [date] NULL,
[Present_Absent] [int] NULL
) ON [PRIMARY]
GO
Imports System.Data
Imports System.Data.SqlClient
Public Class Form7
Public oCn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Uid=sa")
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End Sub
Sub LoadData()
If oCn.State = ConnectionState.Closed Then
oCn.Open()
End If
Dim cmd As New SqlClient.SqlCommand("Select * from mst_employees", oCn)
Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet("bpl")
Dim i As Integer = 0
Me.DataGridView1.Rows.Clear()
Try
da.Fill(ds, "bpl")
If ds.Tables(0).Rows.Count > 0 Then
While (i <> ds.Tables(0).Rows.Count)
Me.DataGridView1.Rows.Add()
Me.DataGridView1.Item(0, i).Value = i + 1
Me.DataGridView1.Item(1, i).Value = ds.Tables(0).Rows(i).Item("Emp_id").ToString
Me.DataGridView1.Item(2, i).Value = ds.Tables(0).Rows(i).Item("Name").ToString
Me.DataGridView1.Item(3, i).Value = ds.Tables(0).Rows(i).Item("Gender").ToString
Me.DataGridView1.Item(4, i).Value = ds.Tables(0).Rows(i).Item("Position").ToString
Me.DataGridView1.Item(5, i).Value = Format(Date.Today, "dd-MMM-yyyy")
i = i + 1
End While
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
Dim oCom As New SqlClient.SqlCommand
Dim oRead As SqlClient.SqlDataReader
If oCn.State = ConnectionState.Closed Then
oCn.Open()
End If
Dim i As Integer = 0
oCom.Connection = oCn
If Me.DataGridView1.Rows.Count > 0 Then
While (i <> Me.DataGridView1.Rows.Count)
oCom.CommandText = "Insert into emp_attendance(Emp_ID,Att_Date,Present_Absent) Values(" & Me.DataGridView1.Item(1, i).Value & ",'" & Me.DataGridView1.Item(5, i).Value & "'," & IIf(Me.DataGridView1.Item(6, i).Value = True, 1, 0) & ")"
oRead = oCom.ExecuteReader()
oRead.Close()
i = i + 1
End While
End If
End Sub
Private Sub btn_checkall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_checkall.Click
Dim i As Integer = 0
If Me.DataGridView1.Rows.Count > 0 Then
While (i <> Me.DataGridView1.Rows.Count)
Me.DataGridView1.Item(6, i).Value = True
i = i + 1
End While
End If
End Sub
End Class
这篇关于我有带复选框的datagridview,复选框表示当管理员检查所有或一些复选框时员工的注意,并根据Datagridview中选中的复选框点击提交按钮...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!