Click here to Skip to main content
15,905,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a datagridview which I use to get input from user.

The problem is that whenever I start inputting the values in any of the cells, a new row is automatically added. I need a new row to be added only when the last cell in the current row has finished with editing.

Also I need to set limit on how many new rows can be added.

Thank you.
Posted
Updated 25-Oct-10 23:06pm
v3
Comments
Dalek Dave 26-Oct-10 5:06am    
Edited for Grammar and Readability.

Set the property of datagridview - AllowUserToAddRows as False

Give a button to add rows.
button click event argument
C#
{
   datatgridview.Rows.Add();
}

if(datagirdview.Rows.Count < n (your limit))
{
   Disable the Button (add row)
}

Thanks
Saranya1388
 
Share this answer
 
v3
Comments
Siddhartha_t69 26-Oct-10 5:24am    
Already tried that.
That gives a problem when i use the calendar column from
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
Look at this, it may help you.
Note: Code in VB, Try to change C#
VB
Set DataGridView1-AllowUserToAddRows as False 
Private Sub DataGridView1_CellContentClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

            If DataGridView1.Rows.Count > 0 Then
                If e.RowIndex >= 0 Then
                    temprowid = e.RowIndex
                 End If
           End If
End Sub
Private Sub DataGridView1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
          //if (e.KeyChar == (char)Keys.Tab) //--From CSharp
           If e.KeyChar.Equals(Keys.Tab) Then //--Check This is Correct or Not
            If temprowid >= 0 Then
               If (DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex()).HeaderText.Equals("LastCellName")) Then
                   BindingSource1.AddNew()
               End If
            End If
          End If
   End Sub
 
Share this answer
 
v3
Comments
Sandeep Mewara 26-Oct-10 6:11am    
Comment from OP:
Can you retype them in c#? also datagridview is not databound.
Pawan Kiran 26-Oct-10 7:32am    
Dear Sandeep, have a look at my C# code. let me know the Comment Regarding my post.
Set DataGridView1-AllowUserToAddRows as False
int temprowid=0;
private void DataGridView1_CellContentClick1(object sender, DataGridViewCellCancelEventArgs e)
{
     if(DataGridView1.Rows.Count > 0 )
     {
        if( e.RowIndex >= 0)
        temprowid = e.RowIndex;
     }
}
private void DataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar == (char)Keys.Tab)
   {
      if(temprowid >= 0) 
      {
if(DataGridView1.Columns[DataGridView1.CurrentCell.ColumnIndex].HeaderText.Equals("LastCellName"))
         FillGrid(1);
      }
   }
}
Private Void FillGrid(int RowCount)
{
     foreach (DataGridViewRow r in DataGridView1.Rows)
     {
        DataGridView1.Rows.Remove(r);
     }
     DataGridView1.DataSource = null;
     if (DataGridView1.Columns.Count > 0)
     {
          DataGridView1.Columns.Clear();
     }
     DataTable dt=new DataTable()
     dt=dc.GetDataTable("Select Statement"); //--Returns DataTable
     if(Rowount > 0)
     {
        For(int i=0;i<RowCount;i++)
        {
           DataRow dr = dt.NewRow();
           dt.Rows.Add(dr);
        }
     }
      DataGridView1.DataSource=dt;
}
 
Share this answer
 
v2
Comments
Siddhartha_t69 27-Oct-10 1:22am    
" dt=dc.GetDataTable("Select Statement"); //--Returns DataTable "

dc is not defined anywhere. Also it is for user input only. It is not databound and hence cannot prepare select statement.
Here is a sample project which contains my problem. A mere 40 KB Donwload.

http://www.mediafire.com/?9nybb7lpni7asq3[^]

Please do check if i used calendar column correctly.
Thank you.
 
Share this answer
 

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