sam
sam

Reputation: 2606

Adding data to DataGridView

I have datagridview and I want to insert in it values that come from textbox a and a NumericUpDown box but I face a problem which is that these values inserted into the datagridview are not in the column that I specified.

Here is the code I wrote:

        dataGridView1.Rows.Add(MaterialTextBox.Text);
        dataGridView1.Rows.Add(HoursNumericUpDown.Value);
        dataGridView1.Rows.Add(MarkNumericUpDown.Value);
        dataGridView1.Rows.Add(MarkPoints);

and by the way is that way of calculating the values that I get from NumericUpDown box is?

        decimal MarkPoints, x, y;
        x = HoursNumericUpDown.Value;
        y = MarkNumericUpDown.Value;
        MarkPoints = x * y;

Because when I write it with this way I got an error

decimal MarkPoints = HoursNumericUpDown.Value * HoursNumericUpDown.Value;

I think there is no different answer for example let's

HoursNumericUpDown value is 2 
MarkNumericUpDown value is 3 

the answer in the first way is 6 which is correct but the answer in the second way is 4. what is wrong?

Upvotes: 1

Views: 1072

Answers (1)

VAShhh
VAShhh

Reputation: 3504

you must specify the cells of your row and then add the row to the dataGridView :

    Dim dgvRow As New DataGridViewRow
    Dim dgvCell As DataGridViewCell

    dgvCell = New DataGridViewTextBoxCell()
    dgvCell.Value = MaterialTextBox.Text
    dgvRow.Cells.Add(dgvCell)

    dgvCell = New DataGridViewTextBoxCell()
    dgvCell.Value = HoursNumericUpDown.Value
    dgvRow.Cells.Add(dgvCell)

    dgvCell = New DataGridViewTextBoxCell()
    dgvCell.Value = MarkNumericUpDown.Value
    dgvRow.Cells.Add(dgvCell)

    dgvCell = New DataGridViewTextBoxCell()
    dgvCell.Value = MarkPoints
    dgvRow.Cells.Add(dgvCell)

    dataGridView1.Rows.Add(dgvRow)

Upvotes: 2

Related Questions