how to connect multiple tables and populate into a datagridview control?
how to connect multiple tables and populate in开发者_如何学JAVAto a datagridview control ? What i can think is create a view, but if there any other better resolutions ?
A view is probably the best simple solution here.
Can you post a sample of your domain model so that we may be of better help?
One thing is for sure: "The simpler the better". Keep it simple! =)
If you need to join output from several tables and show in the gridview than you need to create a view or stored proc returning necessary results and bind its data to the grid.
Instead of binding, I'd use virtual mode. That makes it pretty easy to pull data on-the-fly from different sources. Microsoft has a tutorial on how to use it: How to: Implement Virtual Mode in the Windows Forms DataGridView Control
Essentially, instead of loading all of the data at start up or using a binding source, you hook the CellValueNeeded event. The DataGridView will fire that event whenever it needs to display a cell and you can supply whatever data you want at that time. In your CellValueNeeded handler, you can map the row and column of the DataGridView to your tables however you would like.
private void my_init_function() {
datagridview.VirtualMode = true;
datagridview.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(datagridview_CellValueNeeded);
}
private void datagridview_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
e.Value = get_my_data(e.RowIndex, e.ColumnIndex);
}
精彩评论