Windows Develop Bookmark and Share   
 index > Windows Forms General > LINQ to SQL and DataGridView Databinding woes
 

LINQ to SQL and DataGridView Databinding woes

Hello,
I'm an asp.net developer, primarily using 2.0, so I'm a little behind the pace with both Windows.Forms and LINQ to SQL.

I'm trying to create a form that's used for creating random responses. I have a Listbox that contains a list of the random response groups, and on the SelectedItemChanged event, I'm trying to databind a DataGridView with the random response data in that group. Using a typed DataSet seems fairly straightforward, however I couldn't find a decent enough example that retrieves data based on a parameter value (i.e. the RandomGroupID from the Listbox), so currently I'm setting the datagridview's datasource from my data layer that returns a simple LINQ query.

Code Snippet

private void OnSelectedItemPhraseListBox(object sender, EventArgs e)
{

PhraseGroupGridView.DataSource = m_DataLayer.GetRandomResponses(Convert.ToInt32(PhraseGroupListBox.SelectedValue));
}


public IQueryable<RandomResponse> GetRandomResponses(int randomID)
{

return from rr in m_DataContext.RandomResponses where rr.RandomID == randomID select rr;

}


For displaying and editing data, this suits my purposes just fine, but when I click on the empty row at the bottom to add a new row, I get a DataError, which is easy enough to handle, but I can't edit the cells to add data. I've made my grid virtual and I'm handling the NewRowNeeded event, but I'm not sure what needs to happen here in order for me to add be able to edit the new row's cells to add a new row to the table? And also, could someone give me a quick run down of how I would add a typed dataset that takes a parameter value and rebinds on each event from my Listbox? Thanks, I appreciate any help!
Duck Jones  Saturday, June 28, 2008 3:53 PM

Hi Duck,

Data binding’s “Business Object� which is implemented by BindingList<T>, will fit for you. Please refer to the following code steps:

1. Define data object for each type of response (note that public properties are used as data column in DataGridView):

Code Snippet

// Data object for response 1.

public class ResponseObject1

private int id1;

private string response1;

public int ID1

get { return id1; }

set { id1 = value; }

public string Response1

get { return response1; }

set { response1 = value; }

Code Snippet

// Data object for response 2.

public class ResponseObject2

private int id2;

private string response2;

public int ID2

get { return id2; }

set { id2 = value; }

public string Response2

get { return response2; }

set { response2 = value; }

2. Set DataGridView’s DataSource to specific BindingList generated according to ListBox.SelectedIndex in SelectedIndexChanged event. Of course you can define methods for random generating of each index, and that makes it looks more object oriented (pay attention to the bolded lines):

Code Snippet

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

switch (listBox1.SelectedIndex)

case 0:

BindingList<ResponseObject1> response1 = new BindingList<ResponseObject1>();

for (int i = 0; i < 10; i++)

ResponseObject1 object1 = new ResponseObject1();

"response message: " + i;

this.dataGridView1.DataSource = response1;

break;

case 1:

BindingList<ResponseObject2> response2 = new BindingList<ResponseObject2>();

for (int i = 0; i < 10; i++)

ResponseObject2 object2 = new ResponseObject2();

"response message: " + i;

this.dataGridView1.DataSource = response2;

break;

default:

break;

Let me know if there is any problem.

Thanks.

Best wishes,

Jun Wang

Jun Wang Tim  Thursday, July 03, 2008 7:58 AM

Hi Duck,

Data binding’s “Business Object� which is implemented by BindingList<T>, will fit for you. Please refer to the following code steps:

1. Define data object for each type of response (note that public properties are used as data column in DataGridView):

Code Snippet

// Data object for response 1.

public class ResponseObject1

private int id1;

private string response1;

public int ID1

get { return id1; }

set { id1 = value; }

public string Response1

get { return response1; }

set { response1 = value; }

Code Snippet

// Data object for response 2.

public class ResponseObject2

private int id2;

private string response2;

public int ID2

get { return id2; }

set { id2 = value; }

public string Response2

get { return response2; }

set { response2 = value; }

2. Set DataGridView’s DataSource to specific BindingList generated according to ListBox.SelectedIndex in SelectedIndexChanged event. Of course you can define methods for random generating of each index, and that makes it looks more object oriented (pay attention to the bolded lines):

Code Snippet

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

switch (listBox1.SelectedIndex)

case 0:

BindingList<ResponseObject1> response1 = new BindingList<ResponseObject1>();

for (int i = 0; i < 10; i++)

ResponseObject1 object1 = new ResponseObject1();

"response message: " + i;

this.dataGridView1.DataSource = response1;

break;

case 1:

BindingList<ResponseObject2> response2 = new BindingList<ResponseObject2>();

for (int i = 0; i < 10; i++)

ResponseObject2 object2 = new ResponseObject2();

"response message: " + i;

this.dataGridView1.DataSource = response2;

break;

default:

break;

Let me know if there is any problem.

Thanks.

Best wishes,

Jun Wang

Jun Wang Tim  Thursday, July 03, 2008 7:58 AM

You can use google to search for other answers

Custom Search

More Threads

• please i need help
• Applying ShowCheckMargin to subitems in a ContextMenuStrip
• Removing the application from sytem tray
• how to make form Visible in a "WORD" event handler?
• Adding 4000 child controls to FlowLayoutPanel freezez the scrolling.
• How to create chart/graph in c#?
• User Interface Design Guidelines
• KeyButton ENTER as navigation
• Retrieving a tooltip
• Help about IME of UserControl