Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1347
  • Last Modified:

Textbox in Form2 shows information from DataGridView in Form1

I figured this would be super simple but im having a hard time finding anything on google.

Basically I have a datagridview on Form1. This datagridview shows columns 1 through 3 (lets say there is a 10 datacolumns).
I want to be able to click on the row of the datagridview, display Form2, have 10 textbox's show the information that is bound in formA. and then eventually just update all the information / save.

Basically form1 = gridview, form2 = detailsview.

the datagridview is being populated from a dataset, that is being populated by an xml file.

Thanks!

0
JasonWinn
Asked:
JasonWinn
  • 2
  • 2
2 Solutions
 
rameedevCommented:
What you could do is On click of the datagird row of form1 which has 3 columns (i am assuming you are keeping the Primary Key column as one of the 3 columns)
retreive the PK colimn value do a select from the dataset based on thsi PK get those datarows and use it populate your second grid.
0
 
JasonWinnAuthor Commented:
Hi rameedev,

Thank you your response. Thats exactly what I had in mind, my troubles are finding the code for selecting the dataset based on the pk. And instead of "populating" a 2nd grid, I am just trying to populate a 2nd form containing textbox's. Written below is what I am trying to do, just trying to find the right code for it is what is killing me!

the psuedo code:

//form 1
on click datagridview (form 1)
string row and value's
form2.showdialog

//form2
textbox1.text = value's from datagridview / dataset on form1
textbox2.text = .... // ect various textbox's and controls
change text or update value's of textbox
send new information back to the dataset

0
 
rameedevCommented:
//form1
on the click event of datagrid
you can get the current rownumber and you can hardcode the column number to retreive the PK columns value...put something like this in your datagrid click event

private void dtGrd_Click(object sender, System.EventArgs e)
{
   object cellvalue = dtGrdEmployee[dtGrdEmployee.CurrentRowIndex,0];
   
   //using this cell value as pk retreive data from your dataset..something like this
   DataSet ds = (DataSet)dtGrd.DataSource;
   DataRow[] drArr = ds.Tables[0].Select("PKColumn = '"+ cellvalue +"'");

    string strTxtBox1 = "";
    string strTxtBox2 = "";
    string strTxtBox3 = ""; ///add similar for the No of txtboxes you have in 2nd form

    foreach(DataRow drTmp in drArr)
    {
      strTxtBox1 = drTmp["col1"];
      strTxtBox2 = drTmp["col2"];
      strTxtBox3 = drTmp["col3"];
                ....take all value and assign it to the variables
    }

    //In your form2 overload the constructor to have 10 string arguments
    so when you open the form it can assign the values obtained above directly to it's txtbox
    Form2 objFrm2 =  new Form2("Pass all the values here...strTxtBox1 ,strTxtBox2....and so on")
   
}

//Form2
overloaded Constrcutor
public Form2(string strtxt1,string strtxt2...and so on till whatever values you need)
{
    //after InitializeComponent call
    this.txtbox1.text = strtxt1;
    this.txtbox2.text = strtxt2;
}



0
 
bromoseCommented:
Hi

It can bed done with very littel coding (infact you just have to define some bindings):

If you use a BindingSource, and bind both your datagrid and your textfields to the same instance of BindingSource (make the DataBinding object in form1 and parse it to form2), you will automatic get them in sync.

Then save the dataset to disk.

Hope this helps

Best regards,
Kaj Bromose
0
 
JasonWinnAuthor Commented:
Thank you both for you the help. I am splitting the points because each answers the original question but in different ways.

Sort of related question, I am having some problems filtering with a date string, any help on this would be appreciated it:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_22074261.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now