Reputation: 811
I have a program with many "create new[xxx]" forms (containing textboxes, comboboxes, datetime picekrs etc) that is later saved to the database. To view that data, I use corresponding form "view [xxx]" that has a datagridview filled from database.
When user clicks on the field in the datagridview, data is supposed to be sent back to the original "create" form.
Until now I've been doing it by overloading the constructor of the "create" form by passing the whole row's data from datagridview:
example:
VIEW FORM:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
ID = dataGridView1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
string posName = dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString();
string posDesc = dataGridView1.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
frmNewStaffPosition frm = new frmNewStaffPosition(posName, posDesc);
frm.Show();
frm.NewTypePositionAdded += new frmNewStaffPosition.AddNewTypeOfStaffPosition(frm_NewTypePositionAdded);
CREATE FORM:
public frmNewStaffPosition( string pos, string desc)
{
InitializeComponent();
tbPositionName.Text = pos;
tbNewStaffPositionDecription.Text = desc;
}
While this works, I don't think its the best possible solution, especially if the data passed from the VIEW contains many columns. Is there a more elegant solution, i.e. by passing only PK of selected row and filling the CREATE form straight from database?
If thats matter I use MySQL database through MySQL connector.
Upvotes: 3
Views: 1305
Reputation: 5233
Normally I would go with these underlying rules...
1) if you are find/editing a specific record data, pass only the key data to constructor to the next form.
2) if you are creating a new data with default values, put them to a class.
Upvotes: 0
Reputation: 20330
Depends on what's in charge. Passing they key is one way, but then you'd have to add database code to the form. Turn out to be mess coupling wise that would, and seeing as you are using FormattedValue, you'd have to re-implement that.
You could knock up a class to put the data in and pass that in, and delegate all the to and from stuff to the class, or better still an interface it implements.
Or you could leave it as is, seeing as all these are doing is moving / hiding the messy bit you have now.
If it turns out several of your forms are using common groupings of data, then it might be worth looking at, if not, don't think I'd bother myself.
That said I'd be much more comfortable passing from the datasource underlying the grid, ie the datarow, otherwise for any non-string values you are going to be converting / parsing to or from unless the forms only deal with the data as a string.
Upvotes: 0
Reputation: 203821
If you have less than several hundred columns, as long as your columns aren't BLOBs with several MB of data my guess is this isn't a performance roadblock for your program. If you were passing several hundred (or several thousand) rows then that would be an entirely different issue, but even if you have 200 or so columns passing that much data around for a computer is nothing, it can do that many, many times in a space of time you are incapable of noticing.
Don't prematurely optimize. Is your program running to slow to do its job? If no, don't bother at all, you're done. If it is, use a profiler to see what's taking longer than it ought to and look there for optimizations.
Oh, and if you have more than one or two columns it's probably best to make a class with fields for each column, populate an instance of that class, and then pass it. It's just much easier to deal with than lots and lots of parameters.
Upvotes: 0