Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DatagridView show records of 2 tables using LINQ
 

DatagridView show records of 2 tables using LINQ

Hi all,

I'm trying to implement a Windows Form with the following features:

DatagridView that has Product Category information.

The grid columns are:

Category Name, Tax Class Name, Category Type

I've attached a screenshot of my form. http://a.imagehost.org/0765/frmCategory.jpg

I'm using LINQ to SQL. And I have my DBML file defined like this:

Tax

TaxID
TaxName
TaxValue

Category

CategoryID
CategoryName
CategoryType
TaxID

On DBML I have the correct relationship on it. (see the attachement) http://a.imagehost.org/0515/DBML.jpg

This is my FormLoad

private void frmCategory_Load(object sender, EventArgs e)
{
var category = from c in myDataContext.Categories
join t in myDataContext.Taxes on c.TaxID equals t.TaxID
select new { c.CategoryID, c.CategoryName, c.CategoryType, t.TaxName };


var Tax = from t in myDataContext.Taxes
select t;

cmbTaxType.DataSource = Tax;
cmbTaxType.DisplayMember = "TaxName";
cmbTaxType.ValueMember = "TaxID";

bs = new BindingSource(category, null);

dataGridView1.AutoGenerateColumns = false;

dataGridView1.DataSource = bs;

txtName.DataBindings.Add("Text", bs, "CategoryName");
cmbTaxType.DataBindings.Add("DisplayMember", bs, "Taxes.TaxName");
cmbTaxType.DataBindings.Add("ValueMember", bs, "Taxes.TaxID");


}

My questions:

a) I want to bind the datagrid with the textbox, combobox and radbutton
b) I'm able to bind with textbox and combobox but did not get success on radbutton
c) On my cmdNew_Click button I have a bs.AddNew(). When I try to do it, I receive a error message saying that I'm not able to addnew items on readonly object or size limited.


I'm totally new using LINQ and bindings, so any help here are truly welcome.

Thanks in advance,

Fernando Possebon
Fernando Possebon  Friday, August 21, 2009 1:07 PM
Radio buttons are a bit challenging with binding.

I wrote an article about binding here:

http://www.code-magazine.com/article.aspx?quickid=0603011&page=1

The info on the radio button is on the last page.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, August 21, 2009 3:33 PM
Radio buttons are a bit challenging with binding.

I wrote an article about binding here:

http://www.code-magazine.com/article.aspx?quickid=0603011&page=1

The info on the radio button is on the last page.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, August 21, 2009 3:33 PM

You can use google to search for other answers

Custom Search

More Threads

• Graphic artifacts when horizontally scrolling DataGridView
• two-button custom cell
• Find number from string
• clearing a datagridview
• Help: DGV Combobox settings
• DataGrid: How to hide the right side / present data as a simple table
• VS2005 / VB.NET2005: Datagrid - select line & return data in first cell
• How to bind enum type column in mysql database to a combobox
• Binding an IList to Datagrid
• Disabling some row of DataGridViewComboBoxColumn and DataGridViewButtonColumn