0

it means if user insert "n"(number of case) in textbox than click the "Next" button, in datagridview set to "n" number of rows. i don't know what code i use, please help me,
please check attach file, thank you so much

Edited by Khaliunaa

Attachments 2.png 101 KB
3
Contributors
9
Replies
57
Views
4 Months
Discussion Span
Last Post by Khaliunaa
1

I would set datagrid's property AllowUserToAddRow to False and then manage to add a row by code. For example:

    Private Sub btnNext_Click(sender As System.Object, e As System.EventArgs) Handles btnNext.Click
        Try
            Dim caseNum As Int32 = 0
            If Int32.TryParse(tbCase.Text, caseNum) Then
                With DataGridView1
                    Dim index As Int32 = .Rows.Count
                    .Rows.Add()
                    .Rows(index).Cells(0).Value = caseNum
                End With
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Edited by xrj

0

Thank you so much for your reply,

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int caseNum = 0;
                if (int.TryParse(textBox2.Text, out caseNum))
                {
                    DataGridView dataGridView10 = new DataGridView();
                    int index = dataGridView10.RowCount;
                    dataGridView10.Rows.Add();
                    dataGridView10.Rows[index].Cells[0].Value = caseNum;

                }
               catch (Exception ex)
                {
                throw ex;
                }

is this right? in c#

1

Why do you create a new instance of DataGridView in DataGridView dataGridView10 = new DataGridView(); ?? You should employ the same DataGridView present in the form.

Edited by xrj

0

oh, i forgot

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

                if (int.TryParse(textBox2.Text, out caseNum))
                {
                     nt index = dataGridView10.RowCount;
                    dataGridView10.Rows.Add();
                    dataGridView10.Rows[index].Cells[0].Value = caseNum;

                }

            }

but this code gives me error , Could you tell me which one is wrong?

1

It is always handy for us if you would tell us what the error message was you got.
EDIT:
Just guessing:
Line 11 you could try caseNum.ToString();

Edited by ddanbe: addition

1

Oh, I see.
Well a try statement always has to be followed by at least a catch statement. See here for some explanation.
In your code I'd leave the try out. Your Tryparse method is sufficient here.

0

thank you so much :D ,

 private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                int caseNum = 0;

                if (int.TryParse(textBox2.Text, out caseNum))
                {                 
                    dataGridView10.Rows.Add(caseNum);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

this code is working correctly,
have a nice day.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.