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

Recommended Answers

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

Jump to Post

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

Jump to Post

All 9 Replies

Re: how to set datagridview's row number form textbox? c# 80 80

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
Re: how to set datagridview's row number form textbox? c# 80 80

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#

Re: how to set datagridview's row number form textbox? c# 80 80

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

Re: how to set datagridview's row number form textbox? c# 80 80

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?

Re: how to set datagridview's row number form textbox? c# 80 80

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

Re: how to set datagridview's row number form textbox? c# 80 80

in line 13 ->

    Error   CS1524  Expected catch or finally

this error,

Re: how to set datagridview's row number form textbox? c# 80 80

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.

Re: how to set datagridview's row number form textbox? c# 80 80

There is missing a catch instruction after line #15:

catch  
    {  
    } 
Re: how to set datagridview's row number form textbox? c# 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.