Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with a Datagridview, i do not know ha many rows the datagrid have. Most times will be 1 or 2 record.

In the first cell a have a combobox, and the 5 cell is hiden, contains a unique ID.

I want to transmit the unique ID to a another form for every ROW in the datagridview,

BUT should be 2 cases.

The ID should be send separate if the checkbox is checked or unchecked.

Any sugestion.

i try to use arraylist, but..

I do somentig like that

string[] IDProdusContractPrelugit = new string[dataProduseContract.Rows.Count];
           string[] IDProdusContractRetras = new string[dataProduseContract.Rows.Count];
           for(int i=0; i < dataProduseContract.Rows.Count; i++)
           {

               if (Convert.ToBoolean(dataProduseContract.Rows[i].Cells[0].Value) == false)
                   {
                       IDProdusContractPrelugit[i] = dataProduseContract.Rows[i].Cells[5].Value.ToString();
                   }

               if (Convert.ToBoolean(dataProduseContract.Rows[i].Cells[0].Value) == true)
                   {
                       IDProdusContractRetras[i] = dataProduseContract.Rows[i].Cells[5].Value.ToString();
                   }
           }

           foreach (string a in IDProdusContractPrelugit)
           {
               MessageBox.Show(a);
           }


Now i try to pass the ID to a form2, in this exemple my datagrid contains 4 row, so i ned to pass to the second form 4 values, but, 1 value is from array IDProdusContractPrelugit and 3 from array IDProdusContractRetras.
Posted
Updated 17-Jul-11 3:43am
v2
Comments
Espen Harlinn 17-Jul-11 10:28am    
This is a fairly convoluted way of doing things, databinding lets you "forget" about the internal plumbing of the UI - just read up on it, it will be a fair productivity booster :)

Read through this
How to: Bind Data to the Windows Forms DataGridView Control[^]

Then:
1. Create an entity 'Element' representing the row data, add a bool property for the checkbox
2. Populate an instance of List<element>
3. Assign that instance to the bindingSource1.DataSource

The grid will now apply the changes to your List<element> instance, so you can just work with that - including sending it to the other form.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
thatraja 17-Jul-11 10:54am    
Clean, 5!
Resolved:

FORM 1.

private void btnModificaContractul_Click(object sender, EventArgs e)
        {


            string[] IDProdusContractPrelugit = new string[dataProduseContract.Rows.Count];
            string[] IDProdusContractRetras = new string[dataProduseContract.Rows.Count];

            for (int i = 0; i < dataProduseContract.Rows.Count; i++)
            {

                if (Convert.ToBoolean(dataProduseContract.Rows[i].Cells[0].Value) == false)
                {
                    IDProdusContractPrelugit[i] = dataProduseContract.Rows[i].Cells[5].Value.ToString();
                }
                else
                {
                    IDProdusContractRetras[i] = dataProduseContract.Rows[i].Cells[5].Value.ToString();
                }
            }

           

            try
            {
                frmModificaContract ModificareContract = new frmModificaContract(IDProdusContractPrelugit, IDProdusContractRetras);
                ModificareContract.ShowDialog();
            }
            catch { MessageBox.Show("Trebuie să selectaţi un contract din listă pentru a edita.", "Datele furnizate sunt incomplete."); }
           
        }


FORM 2.

string[] IDProdusContractPrelugit;
        string[] IDProdusContractRetras;

        public frmModificaContract(string[] IDProdusContractPrelugit, string[] IDProdusContractRetras)
        {
            InitializeComponent();
            this.IDProdusContractPrelugit = IDProdusContractPrelugit;
            this.IDProdusContractRetras = IDProdusContractRetras;
            AfisareProduseContract();
        }


        void AfisareProduseContract()
        {

            foreach (string ContractPrelungit in IDProdusContractPrelugit)
            {
                if (ContractPrelungit != null)
                {
                    Program.Connection.CommandText = "select * from ContractItems WHERE ContractItemId=@ContractItemId";
                    Program.Connection.AddParameter("@ContractItemId", ContractPrelungit);

                    DataTable Table = new DataTable();
                    Program.Connection.FillDataTable(Table, true);


                    for (int i = 0; i < Table.Rows.Count; i++)
                    {
                        if (String.IsNullOrWhiteSpace(Table.Rows[i]["DeletedId"].ToString()))
                        {
                            bool validare;
                            validare = true;

                            dataProduseContract.Rows.Add(validare, Table.Rows[i]["Name"].ToString(), Table.Rows[i]["Quantity"].ToString() + " " + Table.Rows[i]["QuantityUnit"].ToString(), Table.Rows[i]["Payment"].ToString(), Table.Rows[i]["Description"].ToString(), Table.Rows[i]["ContractItemId"].ToString());
                        }
                        else
                        {
                            bool validarescos;
                            validarescos = false;
                            dataProduseContract.Rows.Add(validarescos, Table.Rows[i]["Name"].ToString(), Table.Rows[i]["Quantity"].ToString() + " " + Table.Rows[i]["QuantityUnit"].ToString(), Table.Rows[i]["Payment"].ToString(), Table.Rows[i]["Description"].ToString(), Table.Rows[i]["ContractItemId"].ToString());
                            dataProduseContract.Enabled = false;
                        }

                    }
                }
            }

            
        }


The problem is allwais i have Null value in array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900