Home » .Net Framework

How do I link columns in one datagridview to another datagridview

I have 2 datagridviews on different tabs of a tabcontrol.

The user can input names into datagridview1 column1. I want these names to automatically appear in datagridview2 column1, in such a way that when rows in datagridview1 are added and removed the same rows are added and removed in datagridview2. I also want the column sort function to effect datagridview2.




3 Answers Found


Answer 1

the easiest way would be to use an underlying datatable as the datasource for both datagridviews.

If you do this, adding or deleting rows, or sorting a column  in one datagridview  will have the exact same effect  to the other.


Answer 2


Answer 3

Uh, it's poor Forum etiquette to mark your own post as the answer, unless you came up with adifferent solution. Then it helps to post that solution so others with the same problem can find an answer.

Normally you would mark the Post that solved your problem as the Answer and any posts that gave you some helpful information as Helpful



I am new to using VB Express (2008 and 2010). I have an .mdb database with 4 tables. I am able to show a detail form with a couple of comboboxes linked to their related tables so as to show Name rather than the numeric foreign key.

Now I am trying to link the same foreign-key columns in a DGV to show Name and not the key, but can't figure out how to achieve that?

The answer may already be in this forum, but I can't find it!!

Can somebody help me, please?

Cheers, Bill



how to make linkcell editable in datagridview.



hi, i have checked all the suggested threads so i still think the qs i have has not been addressed.

i have a datagridview, having 3columns, col1,col2,col3.

you see the same way we click on a subject in yahoo mail and it opens up the message in another page, i want col2 to be  a link. that i have done but its the functionality that i need to know how and wat to do in order the link to open


Hai friends,

I've a datagridview using C# .net which has been get data fields from MySql and displays like


FirstName    |    LastName   |    ID   


Kelvin          |    David         |   001

Maria           |    John           |   002

Joe              |    Martin         |   003


This above example shows the datas with 3 fields which is in MySql dataBase Table name "Details"

I want to display it in my datagridview as like give below




Kelvin  David  001

Maria  John    002

Joe  Martin 003


This is showing in a single field. And it should be with the same table "Details"

Waiting for Answer...

Thank you....


I have the following code to add a new column into the datagridview but how to default value to this column?

I do not want to use for loop to loop through each row and get the column updated, is there any other way?

I want to default them to 'N' and i will have the code to change it to 'Y' subsequently based on conditions.


        dgDataConfiguration.Columns.Add("Update", "Update")


Many thanks.


In an ASP.NET GridView table, I'm trying to retrieve/display information for each member user---but the columns are stored in 2 different tables.  For example, I want to display the Address, Telephone and UserName (i.e. each user's LoginName) for each unique UserId. The Address and Telephone are stored in aspnet_Membership, but the UserName is stored in aspnet_Users. Both tables are linked by the key column UserId. When I try the statement below, I get the error message in the title of this post (I suppose because each table contains the same UserId column?):

SELECT Address, Telephone, UserId,UserName FROM aspnet_Membership, aspnet_Users

(By the way, I need to include UserId column in the Select statement, because even though I don't publicly display the UserId, I reference the value stored in each UserId in another function on the page.) 

Naturally, I also need to make sure that each table row displays the Address, Telephone and UserName for the same corresponding UserId.

Is another option (preferred) to somehow get the UserName to automatically be stored in a column called UserName in aspnet_Membership at the time the user creates their account/login?  That way, I wouldn't have to call upon aspnet_Users at all. For some reason UserName won't automatically get stored in a UserName column in aspnet_Membership when using the ASP.NET Login control---only gets stored in aspnet_Users table. I don't know how to get that to happen either.

How can I resolve this one way or another?  Many thanks.





I currently have a Choice column in my List and I would like to create a new Choice Column in my List and "link" it to the values in the original Choice column.  That is to say, I do not want to maintain the Choice selections in both columns, I only want to maintain them in one column and have the values automatically appear in the Linked column as well.

Is there a way to do this inside of a List or Document Library.



The List Page display  child Entity using the  gridview each row will have  link to the  parent 


How can I change the link so it shows the parent but in list view(using list.aspx) not detailes (detailes.aspx)view






I checked the




The List Page displays a child Entity using the  gridview, each row  has a to link to the  parent in detailes view how can I change to list view .

use list.aspx?parent=32 not detailes.aspx ?parentId=32


i want fill a datagridview by a list then coloum is image, staring text, link ans string

no have any date base only have a list there have some values

so send me code according my need



I have a DataGridView with a checkbox column.

When the user check a checkbox then the checked value is added to an array.

Unchecking the checkbox removed the item from the array.

This works fine until a checkbox is rapidly checked and unchecked. The array does not get updated when this happens.

Why is this?


Code Snippet

privatevoid myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)


if (e.RowIndex > -1 && this.myDataGridView.Columns[e.ColumnIndex] isDataGridViewCheckBoxColumn)



if (int.Parse(myDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString()) == 1)


string mySelection = myDataGridView.Rows[e.RowIndex].Cells["myColumn"].Value asstring;



elseif (int.Parse(myDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString()) == 0)


string myUnSelected = myDataGridView.Rows[e.RowIndex].Cells["myColumn"].Value asstring;






hello there,

1) I want to add two buttons in one column, how can i do that?

2) i have set dgv property to User can reorder columns, but i dont want first column to be moved

3) in datagridview i have 2 coulumns who must be moved together, how to control this.


thanking you in advance.




I have common sub to save data from data table to database like this


It works fine, but problem comes when i populate Datagridview using a joined query which takes values from more than six tables, for example

        qry = "SELECT rsiteressr, rsitesr, rsitesitesr as [Site ID], cityname as [City], sitelocation as [Location], dispsize as [Size]"

        qry = qry & ", rsiteacqdate as [Date Acq], rsiteexpdate as [Date Exp], DATEDIFF(Day,rsiteacqdate,rsiteexpdate)+1 as [Days], rsitelastrate as [Last Vendor Rate]"

        qry = qry & ", rsiteamount as [Current Vendor Rate],rsitecutout as [Cutout], rsiteillumination as [Illumination],rsiteothercost as [Other Cost], rsitenote as [Remarks]"

        qry = qry & ", siteimageid"

        qry = qry & " FROM subressite"

        qry = qry & " LEFT JOIN site ON  rsitesitesr=sitesr"

        qry = qry & " LEFT JOIN city ON sitecity=citysr"

        qry = qry & " LEFT JOIN dsize ON sitesize=sizesr"

        qry = qry & " WHERE rsiteressr=" & txtID.Text

main table name is subressite where from i populate grid and want to save data in this table. but i also have to show some fields as description NOT editable and not part of table subressite, data for such fields is coming from joined query like cityname, sitelocation etc. and filled in grid,

What should i do to  call ad.Update(tb) method to save data in table and how to handle unbound fileds like, cityname, sitelocation as they are just to display not editable and even they are not defined in table subressite. i have seen an example joined view, but it fills data from all tables which is loss of resources and extra work




I have created a collection of charts in Panel1 control and i want to bind each column in DatagridView control to each of the chart in the collection but using the below code i am able to bind the data from DataGridView column 1 but as the loop iterates there is no change in the charts after Chart1 has been binded with values and gives me data only on Chart1 and rest of the charts are blank whereas there are more than 1 columns with data. There is no error generated.



Dim myReader As OleDbDataReader

myReader = cmd.ExecuteReader()

Dim counts As Integer = 2
        For Each ctrl As System.Windows.Forms.DataVisualization.Charting.Chart In PCharts.Panel1.Controls

          ' Enable range selection and zooming end user interface
          ctrl.ChartAreas("ChartArea1").CursorX.IsUserEnabled = True
          ctrl.ChartAreas("ChartArea1").CursorX.IsUserSelectionEnabled = True
          ctrl.ChartAreas("ChartArea1").AxisX.ScaleView.Zoomable = True

          'set Title
          ctrl.Titles("Chart1").Text = "Name" + vbCrLf + "Date Range"

          'Set Axis points
          ctrl.Series("Series1").Points.DataBindXY(myReader, PCharts.DataGridView1.Columns.Item(0).HeaderText, myReader, PCharts.DataGridView1.Columns.Item(counts).HeaderText)
          ctrl.ChartAreas("ChartArea1").AxisY.Maximum = 100
          ctrl.ChartAreas("ChartArea1").AxisY.Minimum = 0
          ctrl.Series("Series1").ToolTip = "#VALX" + vbCrLf + "#VALY"

          counts = counts + 1

        Next ctrl
      Catch ex As Exception
        If ex.Message <> "" Then
          MsgBox(ex.Message + " Source : " + ex.Source)
        End If
      End Try




Please Help!!!


Hello Experts,

I am facing one difficulty in binding DataGridView in windows form. DataGridView should have three columns EmpName, DeptName, IsOnPayroll. I want to display DeptName in drop Down which should be editable directly and IsOnPayroll is a bit field, it should be displayed in check box and editable too. How can I do it? Herewith I am giving table creation script.


create table empDept
	deptId int identity(1,1),
	deptName varchar(10)

insert into empDept 
select 'MIS' union all
select 'Account' union all
select 'QA'

create table employeeData
	empId int identity(1,1),
	empName varchar(20),
	isOnPayroll bit,
	deptnId int

insert into employeeData
select 'ritesh shah',0,1 union all
select 'rajan jain',1,2 union all
select 'alka shah',1,3

select e.empId,e.empName,d.deptName,e.isOnPayroll from employeeData as e join empDept as d on e.deptnId=d.deptId

I have actually worked very less in windows form so facing this problem. Any help from anybody would be highly appreciated.



hi all,

can any one help me write code to make a combobox editable in a dataGridview.

I'm very new to it. have no much idea.

my requierment is, as i add a row in datagridview say with 2 column [where one column will be a combobox], user should be given a option to edit the combobox and that should be added as a item in the combobox of the datagridview.

plz help me out with this.



took me quite a few searches and experiments to find out I can control and minmize the row selector column still showing the arrow on selected row

// in the load event

myDataGridView.RowHeadersWidth = 24; 
// this is the min w still showing the arrow on the Active row
// not sure if screen resolutiona nd size may affect the the min w
// the default is 41 for .net 3.5 sp1

for those interested in making this column invisibe, use

myDataGridView.RowHeadersVisible = false;


Hi there!

I'd like to use LINQ to find the max cell value in a unbound DataGridView column.

Is it possible?

Many thanks for any kind of help.



I have a database table as follows...

UserID int
Level int
ReportB bit

sample rows in the table is...
User1, 3, 1, 0
User2, 4, 1, 1
User3, 2, 0, 0

I bind the table to a DataGridView.
I want the LevelColumn in the DGV to display values from 1-5 in each row.
These values may not be in the database table.

How do I do this?

And then I want to display database data in the DGV related to that LevelColumn

So for example, the DGV should look like this...

UserCol   |   LevelCol    |  ReportA   |  ReportB  |
User3             2                False          False
User1             3                True           False
User2             4                True           True

Hi all,i have this scenario.I add several unbound columns to datagridview,i've succeeded to copy data from other datagridview to this unbound dgv and also got save database from unbound datagridview to database using tableadapter.insert worked .But i can't figure out how to display the data back into the datagridview with existing column...Does anyone got a tutorial about it?

I've tried using this way :

Dim cn As OleDbConnection = New OleDbConnection_
Dim myTable As DataTable = New DataTable("tblPaketRow" )
myTable.Columns.Add(New DataColumn("ID" ))
myTable.Columns.Add(New DataColumn("Tipe" ))
myTable.Columns.Add(New DataColumn("Nama" ))
myTable.Columns.Add(New DataColumn("Harga" ))
myTable.Columns.Add(New DataColumn("Qty" ))
Dim cmd As OleDbCommand = New OleDbCommand("SELECT fldPackageID,fldPS,fldName,fldRetail,fldQuantity FROM tblPaketRow WHERE
 _(fldPackageID='" & val & "')", cn) Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.Default ) Dim myRow As DataRow While dr.Read() dgvPaketRow.Columns.Clear() myRow = myTable.NewRow myRow.Item(0) = dr(0)
myRow.Item(1) = dr(1) myRow.Item(2) = dr(2)
myRow.Item(3) = dr(3) myRow.Item(4) = dr(4)
myTable.Rows.Add(myRow) End While dr.Close() Dim myData4 As DataTable = myTable ds = New DataSet() ds.Tables.Add(myData4) dgvPaketRow.DataSource = myData4 End If Catch ex As Exception MsgBox(ex.ToString) End Try

It shows the way it should be,but i can't copy data from other datagridview to this dgv...Thanks a lot guys....



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure