user3562155
user3562155

Reputation: 51

Calculating column in datagridview

I have a form and I'm wondering is it possible to calculate a specific column in a datagridview and output it to a label with the use of a generate button? If yes how do I go about doing it?

Thanks

(this is how the datagridview is set up)

Private Sub refreshdata()
        If Not cnn.State = ConnectionState.Open Then
            'open connection
            cnn.Open()
        End If
        'create new data and insert the data, Employee, Firstname, lastname, address etc.
        Dim da As New OleDb.OleDbDataAdapter("SELECT employeeno as [EmployeeID], " & _
                                        "firstname as [FirstName], lastname as [LastName], address as [Address], department as [Department], workinghours as [WorkingHours], datehired as [DateHired], contactnumber as [ContactNumber] " & _
                                        "FROM employee ORDER BY EmployeeNo", cnn)
        Dim dt As New DataTable
        'fill data into datatable
        da.Fill(dt)
        'offer data to be placed in datagridview
        Me.DataGridView1.DataSource = dt
        'close connection
        cnn.Close()
    End Sub`enter code here`

Upvotes: 0

Views: 1056

Answers (1)

msmolcic
msmolcic

Reputation: 6557

I'm not sure what you want to do. If you want just column header in your textBox try this:

C#

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = dataGridView1.Columns["columnName"].HeaderText;
}

VB

Private Sub button1_Click(sender As Object, e As EventArgs)
    textBox1.Text = dataGridView1.Columns("columnName").HeaderText
End Sub

To place your output inside label simply change textBox1.Text to label1.Text. If you want all data written inside your cells of certain column try this:

C#

private void button1_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.IsNewRow)
            break;

        sb.Append(row.Cells["columnName"].Value + " ");
    }

    textBox1.Text = sb.ToString();
}

VB

Private Sub button1_Click(sender As Object, e As EventArgs)
    Dim sb As New StringBuilder()

    For Each row As DataGridViewRow In dataGridView1.Rows
        If row.IsNewRow Then
            Exit For
        End If

        sb.Append(row.Cells("columnName").Value + " ")
    Next

    textBox1.Text = sb.ToString()
End Sub

By the way, column name is the column name in design when you right click your DataGridView > Edit columns under design, there should be a "Name" parameter. Hopefully this covers the answer for your question. Cheers!

EDIT:

Added total sum of column output in textBox solution:

C#

private void button1_Click(object sender, EventArgs e)
{
    int sum = 0;

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.IsNewRow)
            break;

        int number;
        if (int.TryParse(row.Cells["columnName"].Value.ToString(), out number))
            sum += number;
    }

    textBox1.Text = sum.ToString();
}

VB

Private Sub button1_Click(sender As Object, e As EventArgs)
    Dim sum As Integer = 0

    For Each row As DataGridViewRow In dataGridView1.Rows
        If row.IsNewRow Then
            Exit For
        End If

        Dim number As Integer
        If Integer.TryParse(row.Cells("columnName").Value.ToString(), number) Then
            sum += number
        End If
    Next

    textBox1.Text = sum.ToString()
End Sub

Upvotes: 1

Related Questions