使用变量填充Datagridview列 [英] Fill a Datagridview column with a Variable

查看:94
本文介绍了使用变量填充Datagridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用一个变量填充一个列,如果一个checkbox.checked = true则确定。

我添加了3列,AddCol1是Shift(要填充变量,和AddCol2和AddCol3是用户输入的信息。



我想用声明的移位变量填充AddCol1列,我想我需要做一个Cells.Value但是无法获得正确的语法.datagridview有大约100行

这是我正在处理的子。

I am trying to fill a column with a variable that is decided if a checkbox.checked = true.
I have added 3 columns, AddCol1 is Shift (To be filled with the variable, and AddCol2 and AddCol3 to be user entered information.

I want to fill AddCol1 column with the declared shift variable, and I am thinking I need to do a Cells.Value but cannot get the syntax correct. The datagridview has about 100 rows
Here is the sub I am working on.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim sql As String
    Dim datepicked As Date
    datepicked = DateTimePicker1.Text
    'Checkbox Check Loop
    If CheckBox1.CheckState = False And CheckBox2.CheckState = False And CheckBox3.CheckState = False Then
        MessageBox.Show("Please select a Shift")
        Exit Sub
    End If
    If CheckBox1.Checked = True And CheckBox2.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If
    If CheckBox2.Checked = True And CheckBox3.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If
    If CheckBox1.Checked = True And CheckBox3.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If

    'Selection of Shift
    Dim shift As Integer
    If CheckBox1.Checked = True Then
        sql = "SELECT MACHINENUMBER, STYLEWIDTH FROM MachineSet WHERE RunFlag1 = 1"
        shift = 1
    ElseIf CheckBox2.Checked = True Then
        sql = "SELECT MACHINENUMBER, STYLEWIDTH FROM MachineSet WHERE RunFlag2 = 1"
        shift = 2
    ElseIf CheckBox3.Checked = True Then
        sql = "SELECT MACHINENUMBER, STYLEWIDTH FROM MachineSet WHERE RunFlag3 = 1"
        shift = 3
    End If

    'SQL Data connection
    Dim sqlCon1 As New SqlConnection("Data Source=SERVER1\DEV01;database=Production;uid=sa;pwd=passwordhere")
    Dim daSWC1 As New SqlDataAdapter(sql, sqlCon1)
    Dim SQLcmdBuilder1 As New SqlCommandBuilder(daSWC1)
    Dim ds1 As New DataSet

    'Adds the columns Picks, Runtime and Date.
    Dim AddCol1 As New DataGridViewTextBoxColumn
    Dim AddCol2 As New DataGridViewTextBoxColumn
    Dim AddCol3 As New DataGridViewTextBoxColumn

    AddCol1.DataPropertyName = "Shift"
    AddCol1.HeaderText = "Shift"
    AddCol1.Name = "Shift"
    AddCol2.DataPropertyName = "Picks"
    AddCol2.HeaderText = "PICKS"
    AddCol2.Name = "PICKS"
    AddCol3.DataPropertyName = "RunTime"
    AddCol3.HeaderText = "RunTime"
    AddCol3.Name = "RunTime"


    daSWC1.Fill(ds1, "MACHINENUMBER")
    DataGridView1.DataSource = ds1.Tables(0)
    DataGridView1.Columns.Add(AddCol1)' need to autofill with shift variable here
    DataGridView1.Columns.Add(AddCol2)
    DataGridView1.Columns.Add(AddCol3)

End Sub





计划是将此信息写入/附加到另一个表格。



The plan is to write/append this information to a different table.

推荐答案

您要将列添加到DataGr idView1。如果需要,您可以将DataColumns添加到DataTable对象。无论哪种方式,您需要遍历DataTable中的每个DataRow或DataGridView中的每个DataGridViewRow来设置您的值。



如果您使用DataTable路线:

You are adding the columns to your DataGridView1. You could instead add DataColumns to your DataTable object, if you want to. Either way, you need to loop through each DataRow in the DataTable or each DataGridViewRow in the DataGridView to set your values.

if you go the DataTable route:
For Each row as DataRow in ds1.Tables(0).Rows
    If Checkbox1.Checked Then
        row("Shift") = "Value if Checked"
    Else
        row("Shift") = "Value if Not Checked"
    End If
Next





或者如果你继续使用DataGridView



or if you stay with the DataGridView

For Each dgvr as DataGridViewRow In DataGridView1.Rows
    If Checkbox1.Checked Then
        dgvr.Cells("Shift").Value = "Value if Checked"
    Else
        dgvr.Cells("Shift").Value = "Value if Not Checked"
    End If
Next


使用 DataGridViewCheckBoxColumn [ ^ ],但是......



DataGridView可以自动创建列,如果您使用DataTable并将此DataTable数据绑定到DataGridView(DGV)。

当您的数据包含具有位数据类型的字段时(仅存储o和1) ,它应该在DGV列中显示CheckBox。



如需了解更多信息,请参阅:

推进使用DataGridView Checkbox列的基础知识 [ ^ ]
Instead of DataGridViewTextBoxCoumn, use DataGridViewCheckBoxColumn[^], but...

DataGridView can create columns automatically, if you use DataTable and bind this DataTable data to DataGridView (DGV).
When your data contains field with bit data type (stores only o and 1), it should display CheckBox in DGV column.

For further information, please see:
Basics to advance working with DataGridView Checkbox columns[^]


这对我有用。摆脱了班次宣言。谢谢你的帮助。!!!

This is what worked for me. Got rid of the shift declaration. Thanks for the help.!!!
daSWC1.Fill(ds1, "MACHINENUMBER")
DataGridView1.DataSource = ds1.Tables(0)
DataGridView1.Columns.Add(AddCol1)
For Each dgvr As DataGridViewRow In DataGridView1.Rows
    If CheckBox1.Checked Then
        dgvr.Cells("Shift").Value = 1
    ElseIf CheckBox2.Checked Then
        dgvr.Cells("Shift").Value = 2
    ElseIf CheckBox3.Checked Then
        dgvr.Cells("Shift").Value = 3
    End If
Next
DataGridView1.Columns.Add(AddCol2)
DataGridView1.Columns.Add(AddCol3)


这篇关于使用变量填充Datagridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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