Solved

Textbox in Form2 shows information from DataGridView in Form1

Posted on 2006-11-30
5
1,299 Views
Last Modified: 2012-06-22
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
Comment
Question by:JasonWinn
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:rameedev
ID: 18051595
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
 
LVL 3

Author Comment

by:JasonWinn
ID: 18051678
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
 
LVL 5

Accepted Solution

by:
rameedev earned 250 total points
ID: 18051921
//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
 
LVL 3

Assisted Solution

by:bromose
bromose earned 250 total points
ID: 18051955
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
 
LVL 3

Author Comment

by:JasonWinn
ID: 18065323
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now