3

How to get the data of specific rows?

Now, I used a control "DataGridView" to load the data from "local db". How can I read the data of singular row in the DataGridView?

This Question deserves the light of day so here is the process
I have a frmViewChildeTable that has a DataGridView when you click on a row with data you are taken to the frmDetailView
I will post the code for the appropriate process used in both forms

Here is code for the form with the 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

Here is the code that sows the detail view of the record in the DB
Please not the global variable 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

I am not a C# coder but easy to use code converter

Vector
  • 3,066
  • 5
  • 27
  • 54
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37

2 Answers2

2

You could try the following code to solve the problem.

  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);
            }
        }
    }

Regards,

Jack

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
0

I am assuming this is Winforms application. You can simply iterate through the rows using a foreach loop.

foreach (var row in dataGrid.Rows)
{ 
    //Your Code here
}

Also, if you want to get an exact cells data, you can use the index to get to it, say if you want data from 2 row on a column named "Employee"

var employeeExample = dataGrid.Rows[2].Cells["Employee"].Value.ToString();
sin2akshay
  • 333
  • 1
  • 2
  • 11