Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm working on a project and trying to solve same problem on last 2 weeks !
I accepted, i can't solve and need some help from you.

Basically i have a datagrid and an excel file. They have different values.
In excel i have some columns like "report_no_1" or "report_no_1_1" etc. Under this colums some measurements will come from my datagrid.

My datagrid is basically same, just i have 2 columns : Report No and Measurement

I edited report no for get just first number from the string. So i have 2 for report_no_2 or report_no_2_12

Sample Excel Template :

Report_No_1 | Report_no_2_1 | Report_no_2_2 |Report_no_3 | etc.....

Datagrid template :

2 | 21,3
2 | 22,4
3 | 12,1
5 | 11,1
etc.



Thing is very simple. I want to get first number in string from excel cell and compare with my datagrid number for every data in datagrid. If they are equel than need to write measurement to excel file under the report no.

I'm comparing them cause there is some empty space. For example there isn't a 4 value in the datagrid. So i need to check.

Here is my code. If there is just one empty space ( for example there isn't a 4 value in data grid then i need to give an empty space to excel ) it doesn't work first time.

When i checked it seems equal.And just one line later i'm trying to write excel. It wrote to previous column.

Please help me.


akademi is my first column index on excel.


C#
int akademi = 5;
raporBos++;

int toplamStr = dataGridView1.Rows.Count;
for (int i = 0; i < toplamStr-1; i++)
     {
       int nope = Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
       double olcumi = double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
       int abc = 0;
       int cag;
       int gac;
       if (!String.IsNullOrEmpty(raporAralik.Cells[raporBasSatZ, akademi].Value))
           {
            string[] cagF = (raporAralik.Cells[raporBasSatZ, akademi].Value.ToString().Substring(8)).Split('_');
            cag = Int32.Parse(cagF[1]);
            gac = Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
            Debug.WriteLine(gac + " gac || " + cag + " cag");
            int osman = gac - cag;
            while (cag < gac)
                  {
                   Debug.WriteLine("*** "+gac + " gac || " + cag + " cag");
                   akademi++;
                   if (!String.IsNullOrEmpty(raporAralik.Cells[raporBasSatZ, akademi].Value))
                       {
                        string[] cagG = (raporAralik.Cells[raporBasSatZ, akademi].Value.ToString().Substring(8)).Split('_');
                        cag = Int32.Parse(cagG[1]);
                       }

                   abc++;
                   if (abc == 100)
                        {
                         break;
                        }
                    }
                                    
            raporWrkS.Cells[raporBos, akademi] = Math.Round(olcumi, 3);
            akademi++;

       }

       else
           {
            break;
           }
}


What I have tried:

while loop, if statement, for loop
Posted
Updated 25-Jul-17 12:02pm
v3
Comments
Graeme_Grant 25-Jul-17 17:22pm    
It would be easier to bind a collection of objects to the DataGrid and do the work directly with the collection...
kozmikadam 25-Jul-17 17:25pm    
I'm open for all suggestion. But couldn't understand what you mean. Can you show a sample ?
Graeme_Grant 25-Jul-17 17:29pm    
The UI (user interface) is a viewport to the data for the user. The DataGrid is a view/edit user interface to the data. You are trying to do BL (business logic) in the UI layer.

So build a model of the data and then bind the UI (DataGrid) to the model. Once you have done this, the task that you want to perform is then simple and straight forward.

See an example of this below in Solution 2...

An example of Binding to Data as requested...
C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace WinFormDataGridBinding
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindData();
        }

        private void BindData()
        {
            Persons.Add(new Person { Name = "Joe", Surname = "Smith" });
            Persons.Add(new Person { Name = "Jane", Surname = "Doe" });

            PersonsBindingList = new BindingList<Person>(Persons);
            dataGridView1.DataSource = new BindingSource(PersonsBindingList, null);
        }

        public List<Person> Persons { get; } = new List<Person>();
        public BindingList<Person> PersonsBindingList { get; private set; }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }
}
 
Share this answer
 
Not a solution to your question, but something you might learn.
Proper indentation helps reading code and show the structure of your code.
C#
int akademi = 5;
raporBos++;

int toplamStr = dataGridView1.Rows.Count;
for (int i = 0; i < toplamStr-1; i++)
{
    int nope = Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
    double olcumi = double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
    int abc = 0;
    int cag;
    int gac;
    if (!String.IsNullOrEmpty(raporAralik.Cells[raporBasSatZ, akademi].Value))
    {
        string[] cagF = (raporAralik.Cells[raporBasSatZ, akademi].Value.ToString().Substring(8)).Split('_');

        cag = Int32.Parse(cagF[1]);
        gac = Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
        Debug.WriteLine(gac + " gac || " + cag + " cag");
        int osman = gac - cag;
        for (int l = 0; l < osman; l++)
        {
            akademi++;
        }
        /*
        while (cag < gac)
        {
            Debug.WriteLine("*** "+gac + " gac || " + cag + " cag");
            akademi++;
            if (!String.IsNullOrEmpty(raporAralik.Cells[raporBasSatZ, akademi].Value))
            {
                string[] cagG = (raporAralik.Cells[raporBasSatZ, akademi].Value.ToString().Substring(8)).Split('_');

                cag = Int32.Parse(cagG[1]);
            }

            abc++;
            if (abc == 100)
            {
                break;
            }
        }
        */
        raporWrkS.Cells[raporBos, akademi] = Math.Round(olcumi, 3);
        akademi++;
    }
    else
    {
        break;
    }
}

A programmer's editor like NotePad++ have such function Visual Studio too.
Notepad++ Home[^]
 
Share this answer
 
Comments
Graeme_Grant 25-Jul-17 17:50pm    
5++ :)
Patrice T 25-Jul-17 18:17pm    
Thank you

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