Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > add row in datagridview
 

add row in datagridview

i want to add row in datagridview if item not exist. for eg

col1
------------
Ather |
------------
Bilal |
------------
if "ather" already exist in the gridview than not add any row.

this is my code where i want to check (in dataTable1 name appear multiple times)

Grid_StudentName.Rows.Add(dtStudentNames.Rows(k).Item(

"Student_Name").ToString)

thanks



Ather
Ather.Abbas  Tuesday, August 18, 2009 5:06 AM

In my opinion a PrimaryKey for the Table underlying the DataGridView (DataSource of the Grid) should be the way to go.
No need to add the Items manually as you do - simply set the DataSource of the DataGridView to be the dtStudentNames dataTable (i assume a DataTable as id is prefixed dt).
On the Other hand you dataTable should get a PrimaryKey on Studen_Name column.
This will absolutly ensure uniqueness without much of an efford.

Christoph Wagner  Tuesday, August 18, 2009 5:59 AM
Well that was not you initial request where you stated only one grid but you may be able to leverage the Select() method from the dataTable unterlying the Grid_RevenueReceipt. This will either return rows for the Student_name or not, so you can find out whether to add or not.
Christoph Wagner  Tuesday, August 18, 2009 7:16 AM

In my opinion a PrimaryKey for the Table underlying the DataGridView (DataSource of the Grid) should be the way to go.
No need to add the Items manually as you do - simply set the DataSource of the DataGridView to be the dtStudentNames dataTable (i assume a DataTable as id is prefixed dt).
On the Other hand you dataTable should get a PrimaryKey on Studen_Name column.
This will absolutly ensure uniqueness without much of an efford.

Christoph Wagner  Tuesday, August 18, 2009 5:59 AM

Ineed to check if column(0) not contains student name then the following line execute

Grid_StudentName.Rows.Add(dtStudentNames.Rows(k).Item("Student_Name").ToString)


this is sample code what i want

If

Grid_RevenueReceipt.Rows.Contains(dtStudentNames.Rows(k).Item("Student_Name").ToString) = False Then

Grid_StudentName.Rows.Add(dtStudentNames.Rows(k).Item("Student_Name").ToString)

End

If



Ather
Ather.Abbas  Tuesday, August 18, 2009 6:27 AM
Well that was not you initial request where you stated only one grid but you may be able to leverage the Select() method from the dataTable unterlying the Grid_RevenueReceipt. This will either return rows for the Student_name or not, so you can find out whether to add or not.
Christoph Wagner  Tuesday, August 18, 2009 7:16 AM
If you can bind datatable to datagridView then you can do following things:
Best option is,you shouldapply distinct in yourSQL query,

otherwise you can select distinct from your datatable
DataTabel.DefaultView.ToTable(true,/*col1*/)
NareshG  Tuesday, August 18, 2009 8:11 AM

You can use google to search for other answers

Custom Search

More Threads

• Validation within a data grid
• DataGridView ComboBox column - formatted items
• AcceptChangesDuringUpdate
• Problem with TypedDataset and webservice
• showing only date part of datetime in datagridview control.
• Using OpenfileDialog and combobox controls in Datagridview cell.
• Disabling small trangular glyphs in the row header
• Inserting a default value into a formview
• Personalize a datagrid
• [VB.NET] DataTable: Add new row at specific position