Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I have a datagridview and during runtime i have to add rows in that datagridview.

How can i add?

Can anyone help me to solve this.

I am new to vb.net. am using c# in vb.net
Posted
Updated 20-Jul-11 8:16am
v2

// Use ProcessCmdKey Befor Form_Load
// Set AllowUserToAdd Row To true

C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            int iCol = dtg.CurrentCell.ColumnIndex;
            int iRow = dtg.CurrentCell.RowIndex;

            if (iCol == dtg.Columns.Count - 1)
            {
                if (iRow == dtg.Rows.Count - 1)
                {

                  // Based On Columns Numbers That You Are Declared In DataGridView
                    dtg.Rows.Add("FirstValue","SecondValue"," . . .");

                }
                dtg.CurrentCell = dtg[0, iRow + 1];
            }
            else
            {
                dtg.CurrentCell = dtg[iCol + 1, iRow];
            }
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
 
Share this answer
 
I think you are confusing your languages there...you are either coding in the language of C# or VB. The .Net part is the framework. In any case, it's usually pretty easy to convert code between the two.

And about your question, try google[^]. Or here[^] is an msdn tutorial on the datagridview that may help.
 
Share this answer
 
You could try something like -
DataGridView1.Rows.Insert(rowPosition, New String(){value1, value2, value3....})
 
Share this answer
 
Comments
Hend Riad 17-Jul-13 6:26am    
There's no method with this name "Insert" too

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900