Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to bind fields of DataGrid with values from other tables?
 

How to bind fields of DataGrid with values from other tables?

Let me explain the situation..

I have a datagrid with values coming from a table. This table has a number of non-descriptive columns. How could when grid is displayed, these non-descriptive values can be replaced with descriptive vales read from a different table.

For example, the table's columns are

LocationId, StateId, CityId
HY1 AL HY
HY9 MNYU

The descriptive values of these mnemonics are stored in different tables, one for location, one for state and one for city.

How the values can be automatically transferred to the grid without using a complex join? I want a simple sql so that records can still be inserted into table using dataadapter.

Any help is appreciated.


___________________________________________________ Thanks anandnairv - Always learning something...
  • Moved byeryangMSFTWednesday, September 09, 2009 7:56 AM (From:.NET Base Class Library)
  •  
anandnairv  Monday, September 07, 2009 3:06 PM

I don't think without join query you can display meaningful name in the grid.


If by chance you are interested in join query then you can use following one

SELECT L.LocationName, S.StateName, C.CityName 
FROM [MainTable] M
JOIN Location L ON L.LocationID = M.LocationID
JOIN State S ON S.StateID = M.StateID
JOIN City C ON C.CityID = M.CityID

Gaurav Khanna
Khanna Gaurav  Monday, September 07, 2009 5:09 PM
Thank Khanna,
But with a join, if I configure a dataadapter, all DML commands wont be generated, I think. What I mean is only Select/Delete command would be generated. But, If records are to be inserted/updated on the MainTable using the same adapter, how would it then be possible?
___________________________________________________ Thanks anandnairv - Always learning something...
anandnairv  Monday, September 07, 2009 5:14 PM

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class DataTableForm : Form

{

#region Do not modify code in this region

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dataGridView1 = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();

this.SuspendLayout();

//

// dataGridView1

//

this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;

this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;

this.dataGridView1.Location = new System.Drawing.Point(0, 0);

this.dataGridView1.Name = "dataGridView1";

this.dataGridView1.Size = new System.Drawing.Size(292, 266);

this.dataGridView1.TabIndex = 0;

this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);

//

// DataTableForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.dataGridView1);

this.Name = "DataTableForm";

this.Text = "DataTableForm";

this.Load += new System.EventHandler(this.DataTableForm_Load);

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;

#endregion

DataTable dataTable;

BindingSource bindingSource;

public DataTableForm()

{

InitializeComponent();

this.Load+=new EventHandler(DataTableForm_Load);

this.dataTable = new DataTable();

}

public DataTableForm(DataTable table)

: this()

{

table.Locale = System.Globalization.CultureInfo.InvariantCulture;

this.dataTable = table;

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}

private void DataTableForm_Load(object sender, EventArgs e)

{

this.bindingSource = new BindingSource();

this.bindingSource.DataSource = this.dataTable;

this.dataGridView1.DataSource = this.bindingSource;

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

return;

}

}

}

That demonstrates how to bind a DGV to a DataTable.


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Monday, September 07, 2009 11:21 PM
Hi anandnairv,

This requirement is not easy to achieve. Here is my confusion with the requirement.

1. Once you see the descriptive text, how can you edit them and save to the database?
2. The table in the database is non-descriptive, if you save edited data into database, the value in the table will be descriptive. How can you handle the problem?

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, September 10, 2009 2:50 AM

You can use google to search for other answers

Custom Search

More Threads

• AddNew() gives wrong value on Unique Identity column
• How to add the checkbox in a DataGrid using Windows Forms [ 2.0 Framework]?
• Altering and/or dropping Wizard-Created binding sources
• ErrorProvider indicator icon not showing
• How to modify DataGridView values before they are validated
• Adding TableAdapters to a form
• changing editable DataGridView into not editable
• How do I save a null value to an int column in a SQL database?
• Loading unique values to a combobox and ...
• DataGridView and Background worker