4 Replies - 490 Views - Last Post: 26 September 2013 - 07:11 AM Rate Topic: -----

#1 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Passing Datagridview items into a list and access anywhere.

Posted 26 September 2013 - 05:04 AM

Hi.. i ran into trouble trying my new project using c# and i'm fairly new to c# in fact this is just my second project with this language ever since.
Here is the scenario.
i have a normal search form from my main form and an advanced search form as another 1 so that the user can have more parameters to be used as what suits his need for any output he desired to.
The advanced search form is modal.
in the advncd search from i have 3 buttons 1st is for the search_button and the 2nd is for the user to move the selected items from the datagridview to the datagridview in the main form and the last 1 is to move all the items from the datagrid.
I was able to figure out how to add and retrieve the added data into the list but i'm having trouble accessing it from the main form.
here is what i've done so far. please take a look and give some advice if you have any idea that might help me walk into the right path :)/>
Code:

'This is where i store the data from the datagrid in my advanced search form
public class dgvItems
    {
       public  string VendorName { get; set; }
       public  string ItemNo { get; set; }
       public  string Model { get; set; }
       public string SN { get; set; }
       public string RefNo { get; set; }
       public dgvItems(string venName,string itmNo,string mdel,string sn,string refNo) 
       {
           this.VendorName = venName;
           this.ItemNo = itmNo;
           this.Model = mdel;
           this.SN = sn;
           this.RefNo = refNo;
       }
    }


'This is what i did to make it accessible within the advanced search form

public partial class frmAdvanced : Form
    {

        private List<dgvItems> itms = new List<dgvItems>();
        public string SearchVal 
        {
            get { return txtAdvanced.Text; }
            
        }
        public frmAdvanced()
        {
            InitializeComponent();
            
            
        }

'Adding items to the list
    private void btbMoveSel_Click(object sender, EventArgs e)
        {
            itms.Clear();
            for (int i = 0; i < dgvAdvancedResult.RowCount - 1; i++)
            {
itms.Add(new dgvItems(dgvAdvancedResult.Rows[i].Cells[0].Value.ToString(), dgvAdvancedResult.Rows[i].Cells[1].Value.ToString(), dgvAdvancedResult.Rows[i].Cells[2].Value.ToString(), dgvAdvancedResult.Rows[i].Cells[3].Value.ToString(), dgvAdvancedResult.Rows[i].Cells[4].Value.ToString()));
            }
}
}



if i'll access it from my main form i will need to instantiate the list again which will probably erase all the previously stored data.

i hope somebody could help me figure this out.
Thanks in advance. :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Datagridview items into a list and access anywhere.

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Passing Datagridview items into a list and access anywhere.

Posted 26 September 2013 - 06:12 AM

Your GUI elements should only be displaying data, not holding it. You should have an object that is the source of the data; that way, it can be made available to any form or user control without fear of it being lost when a window is disposed.

My guess here is that you're writing software as a way to learn. I'd like to urge you to try the Learning C# Series of tutorials here, as well as any other tutorials or books you can get your hands on. Guided learning will help you do things the right way, not just a way that works. By the right way, well, you could wash your bicycle with a toothbrush, but wouldn't you prefer to use a jet wash? If you write software components the wrong way, sure, they might work, but are they working too hard?
Was This Post Helpful? 1
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • Joined: 08-April 09

Re: Passing Datagridview items into a list and access anywhere.

Posted 26 September 2013 - 06:13 AM

private List<dgvItems> itms = new List<dgvItems>();
public List<dgvItems> GetList() //This is what you already have, just created GetList method
    {
        return itms;
    }

And the in your another class

public class CallingClass {

    Form1 frm1= new Form1();

    public void GetList()
    {
        List<dgvItems> calledList = frm1.GetList(); //GetList will return any data that is contained in your itms List
       
    }
}

Was This Post Helpful? 1
  • +
  • -

#4 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Passing Datagridview items into a list and access anywhere.

Posted 26 September 2013 - 06:38 AM

thanks for the quick reply Michael. though i've figured it out by surfing through this forum and found useful tutorials.
here is what i did. i instantiated my 2ndform form my main form and set my dgvItems list to public and added frmclosed event handler to my 2nd form. the retrieve any data that has been added to the list..
here's how it looks for the sake of others that might end up asking same question.
//from my 2nd form
public List<dgvItems> itms = new List<dgvItems>();

event handler from my main form

public partial class frmMain : Form
    {
        private frmAdvanced frmadv;
        
       
        public frmMain()
        {
            InitializeComponent();
        }
//event Handler for the 2nd form
    void frmadv_FormClosed(object sender, FormClosedEventArgs e)
       {
           txtSearch.Text = frmadv.SearchVal;
           dgvSearchResult.Rows.Clear();
//this is where i retrieve data from the list
           for (int i = 0; i < frmadv.itms.Count - 1; i++ )
           {
               dgvSearchResult.Rows.Add(frmadv.itms[i].VendorName,frmadv.itms[i].ItemNo,
                   frmadv.itms[i].Model,frmadv.itms[i].SN,frmadv.itms[i].RefNo);
           }
       }
//this is where i used the frmclosed handler to
       private void btnAdvance_Click(object sender, EventArgs e)
       {
           frmadv = new frmAdvanced();
           frmadv.FormClosed += new FormClosedEventHandler(frmadv_FormClosed);
           frmadv.ShowDialog(this);
        }

    }



thank you so much for the assistance michael :)
+1
Was This Post Helpful? 0
  • +
  • -

#5 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Passing Datagridview items into a list and access anywhere.

Posted 26 September 2013 - 07:11 AM

View PostMrShoes, on 26 September 2013 - 06:12 AM, said:

Your GUI elements should only be displaying data, not holding it. You should have an object that is the source of the data; that way, it can be made available to any form or user control without fear of it being lost when a window is disposed.

My guess here is that you're writing software as a way to learn. I'd like to urge you to try the Learning C# Series of tutorials here, as well as any other tutorials or books you can get your hands on. Guided learning will help you do things the right way, not just a way that works. By the right way, well, you could wash your bicycle with a toothbrush, but wouldn't you prefer to use a jet wash? If you write software components the wrong way, sure, they might work, but are they working too hard?



thank you so much for the advice MrShoes.
i'll take it. and try to learn more about c# "The right way"
on the other hand i'm creating this solution as requested by my employer
this is a serial number and vendor monitoring solution where user could easily trace back were the specific items to be returned just in case they are defective and needs to avail RMA/warranty support.
the RMA system that i've made has been done with vb.net so as the other solutions that i've made for the company that i'm currently working at.
i've been trying to learn c# and found the chance to try to make a solution that will really be used and find some more challenging hindrances that may arise as i go on with the process.
and here it came. vb.net is fine but isn't it more fun if we know how to communicate in several languages. and much better, just like you said if we can do it "The Right Way".
Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1