c# - 如何获取DataGridView中的数据?

标签 c# datagridview

<分区>

如何获取特定行的数据?

现在,我使用控件“DataGridView”从“本地数据库”加载数据。如何读取DataGridView中单行的数据?

这个问题值得一提,所以这是过程
我有一个 frmViewChildeTable,它有一个 DataGridView,当您单击包含数据的行时,您将被带到 frmDetailView
我将发布两种形式中使用的适当流程的代码

这是带有 DataGridView 的表单的代码

  Public Sub dgvChild_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvChild.CellMouseClick

    If e.RowIndex = -1 Or e.ColumnIndex = -1 Then
        'tbMessage.Text = "Col " & e.ColumnIndex & " ROW " & e.RowIndex'FOR TESTING
        tbMessage.Text = "No Clicking Here"
        Return
    End If

    Dim s1 As Integer
    Dim str1 As String

    Dim row As DataGridViewRow = dgvChild.Rows(e.RowIndex)
    str1 = dgvChild.CurrentRow.Cells(0).Value.ToString
    If str1 IsNot " " Then
        If str1 Is " " Then
            Return
        End If
        tbMessage.Text = "Text Sent to RTB"
        s1 = CInt(row.Cells(0).Value)
        gv_childInt = s1     'Set gv_childInt
        frmDetailView.Show() '<========= LOOK
        Close()
        Return
    End If

    tbMessage.Text = "No Data Here"

End Sub

这是在数据库中显示记录的详细 View 的代码
请不要全局变量 gv_childInt

    Private Sub readChildTableID()

    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
        conn.Open()
        'The $ sign and '{String or Integer}' how to add variable to SQL Select Statement
        'Must incorparate BOTH for SELECT to work
        '=================================================================================
        Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT * FROM ChildTable  WHERE CID = '{gv_childInt}'", conn)
            Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader

                While rdr.Read()
                    tbMessageView.Text = rdr("CID").ToString
                    tbDispDateView.Text = rdr("cDispDate").ToString
                    tbTitleView.Text = rdr("cTitle").ToString
                    rtbEnterView.Text = rdr("cEntry").ToString
                End While
                rdr.Close()
            End Using
        End Using
        conn.Close()
    End Using
    Me.Text = "Notebook Entry for " & tbDispDateView.Text
    'CODE above sets text in Title Bar
    '==================================
End Sub

我不是 C# 程序员,但易于使用的代码转换器

最佳答案

您可以尝试以下代码来解决问题。

  private void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if((i+1)%2==1)
            {
               string m= dataGridView1.Rows[i].Cells[1].Value.ToString();
                Console.WriteLine(m);
            }
        }
    }

问候,

jack

关于c# - 如何获取DataGridView中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321037/

相关文章:

c# - 如何使用 CaSTLe Windsor 与 mvc Web 应用程序不同的程序集?

c# - 我想生成具有角标(Badge)和声音功能的 iOS 推送通知有效负载,我可以使用哪些类和库? [C#]

vb.net - 创建动态 DataGridViewComboBoxCell

vb.net - 如何刷新 DataGridView?

c# - 如何将来自 Web 服务的阿拉伯语值存储在数据库中

c# - Xamarin 表单和 Android 蓝牙

c# - 从 ASP.NET Web API 层使用的方法中抛出或不抛出异常

c# - 禁用鼠标与 RichTextBox 的交互(所有鼠标事件对 RichTextBox 没有影响)

c# - 从绑定(bind)到 DataGridView 的 DataTable 中删除最后一个 DataRow 时如何防止出错?

c# 如果触发 DataError 则获取单元格文本值