Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > datagridview newrow()
 

datagridview newrow()

HI,

in which event datagridview's newrow is fired

venp--

venp  Monday, January 08, 2007 5:51 PM

hey venp... check this out when you get a chance. May get you closer. Just create a new windows application and paste the following:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

loadme()

End Sub

Function loadme()

With DataGridView1

.Columns.Add("Col1", "Column One")

.Columns.Add("Col2", "Column Two")

.Rows.Add(" ")

.AllowUserToAddRows = False

End With

End Function

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

' set values for grabbing cell in grid

Dim rowIndex As Integer = DataGridView1.CurrentCellAddress.Y 'This will get the row index of the current selected cell

Dim row1 As DataGridViewRow = DataGridView1.Rows(rowIndex)

Dim reqSelectCol1 As String = row1.Cells(0).Value.ToString 'Get the value of the first cell on the row selected

'Dim reqSelectCol2 As String = row1.Cells(1).Value.ToString 'Get the value of the second cell on the row selected

'__USER CHANGED "Column One" cell value IN GRID________________________________________________________'

If DataGridView1.CurrentCellAddress.X = "0" = True Then

DataGridView1.Rows(e.RowIndex).Cells(1).Value = reqSelectCol1

End If

End Sub

End Class

hope this is a start for you...

billb59  Wednesday, January 10, 2007 8:41 PM

do you mean rows added.... if so...

Private Sub dbGrid_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles dbGrid.RowsAdded

hope this helps.

billb59  Monday, January 08, 2007 11:30 PM

HI,

I've a requirement like there should be only one row in a grid. i can display one row with data or i can type and create a row. after 1st row newrow shold not be created. i need to trap this. at which time new row is created. like as we start typing in a row while pressing enter key new row is created. As soon as the new roww is created i need to trap this and stop creating newrows.

thanks

venp--

venp  Tuesday, January 09, 2007 1:47 AM

 

not sure if i read your above correctly... setting the AllowUsersToAddRows to false will stop the addition of new rows....

YourDataGridView.AllowUserToAddRows = False

hope this helps...

billb59  Tuesday, January 09, 2007 1:58 AM

HI,

I need to use the above command after adding 1st row and whenever editing is done. like No more rows. after 1st row.

I need to know exactly where i've to call the above statement.

venp--

venp  Tuesday, January 09, 2007 6:34 PM

so where is your call to populate the grid made... do you have a code sample you can post? you could control it from your sql statement as well... ie... SELECT TOP (1)from yourtable

billb59  Tuesday, January 09, 2007 7:32 PM

I think the code below maybe do some help to u.

it is about how to prevent the datagrid from displaying its append row (the row at the end with an asterisk)

 

 

The DataGrid class does not have a property that controls whether a new row can be added. But the DataView class does have such a property (along with some others such as AllowEdit and AllowDelete). Here is code that will turn off the append row by getting at the dataview associated with the datagrid.

 

     string connString = @"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\northwind.mdb";

 

     string sqlString = "SELECT * FROM customers";

 

 

 

     // Connection object

 

     OleDbConnection connection = new OleDbConnection(connString);

 

 

 

     // Create data adapter object

 

     OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlString, connection);

 

 

 

     // Create a dataset object and fill with data using data adapter's Fill method

 

     DataSet dataSet = new DataSet();

 

     dataAdapter.Fill(dataSet, "customers");

 

               

 

     // Attach dataset's DefaultView to the datagrid control

 

     dataGrid1.DataSource = dataSet.Tables["customers"];

 

 

 

     //no adding of new rows thru dataview...

 

     CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];     

 

     ((DataView)cm.List).AllowNew = false;

 

 

If your datagrid contains links, then Matthew Miller suggest adding Navigate handler such as the one below to disallow the AddNew.

 

private void DataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)

 

{

 

     if(ne.Forward)

 

     {

 

          CurrencyManager cm = (CurrencyManager)BindingContext[DataGrid1.DataSource,DataGrid1.DataMember];

 

          DataView dv = (DataView) cm.List;

 

          dv.AllowNew = false;

 

     }

 

}

 

Gavin Jin - MSFT  Wednesday, January 10, 2007 2:55 AM

hi bill,

i'm providing the first row value by typing in the grid directly. then i should not be able to add any new rows. first row can be empty no need to have data. in grid i'm providing trasaction details which can have max of 1 record. like 1-1 with master-transaction. instead of getting input using text boxes,i need to use grid as per my requirement .hope this might be little bit more clear.

thanks

venp--

venp  Wednesday, January 10, 2007 5:53 PM

hey venp... check this out when you get a chance. May get you closer. Just create a new windows application and paste the following:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

loadme()

End Sub

Function loadme()

With DataGridView1

.Columns.Add("Col1", "Column One")

.Columns.Add("Col2", "Column Two")

.Rows.Add(" ")

.AllowUserToAddRows = False

End With

End Function

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

' set values for grabbing cell in grid

Dim rowIndex As Integer = DataGridView1.CurrentCellAddress.Y 'This will get the row index of the current selected cell

Dim row1 As DataGridViewRow = DataGridView1.Rows(rowIndex)

Dim reqSelectCol1 As String = row1.Cells(0).Value.ToString 'Get the value of the first cell on the row selected

'Dim reqSelectCol2 As String = row1.Cells(1).Value.ToString 'Get the value of the second cell on the row selected

'__USER CHANGED "Column One" cell value IN GRID________________________________________________________'

If DataGridView1.CurrentCellAddress.X = "0" = True Then

DataGridView1.Rows(e.RowIndex).Cells(1).Value = reqSelectCol1

End If

End Sub

End Class

hope this is a start for you...

billb59  Wednesday, January 10, 2007 8:41 PM

You can use google to search for other answers

Custom Search

More Threads

• Is it a DataGrid?
• DataGrid Question
• DataGridView - BeginUpdate
• BindingNavigator
• Using OpenfileDialog and combobox controls in Datagridview cell.
• Auto-generating databound controls.
• SSRS export to PDF
• Problem with Listbox and Menuitem !
• Null values being displayed
• DataGridColumn format