0

I want to get row number of my excel data. Here, I have excel file that has data like below.

I have data :

WSID  Nominal
123   1000
456   2000
789   3000
987   4000
654   5000
321   6000

I want to show row number of my data that is shown in DatagRidView. How can I do that ?

string koneksi = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Program-Pengisian-Uang-ATM-BCA-SOY\Program-Pengisian-Uang-ATM-BCA-SOY\bin\x86\Debug\ATM SLA Surabaya.xlsx; Extended Properties='Excel 12.0 xml;HDR=YES;'";

    private void PrintScheduleBtn_Click(object sender, EventArgs e)
    {
    Excel.Application oApp;
    Excel.Worksheet oSheetCartridge;
    Excel.Workbook oBook;

    oApp = new Excel.Application();
    oApp.Visible = true;
    oApp.DisplayAlerts = false;

    oBook = oApp.Workbooks.Open(lokasifile, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
    oSheetCartridge = oBook.Sheets["Cartridge"];

    OleDbConnection kon2 = new OleDbConnection(koneksi); //database
    OleDbCommand comm = kon2.CreateCommand();

    foreach (DataGridViewRow row in JadwalisiGV.Rows)
    {
        if (!row.IsNewRow)
        {
        kon2.Open();    
        comm.CommandText = "SELECT * FROM [Data$] WHERE [WSID] = '"+ row.Cells["WSID"].Value +"'";
        listBox1.Items.Add(); // I want to show row number of my WSID data that is shown in JadwalisiGV here
        kon2.Close();

        }
    }

    } 
1
  • Your qus is not clear.. From where u want row no. from datatable? Commented Dec 18, 2016 at 3:23

2 Answers 2

1

You can try this code

foreach (DataGridViewRow row in JadwalisiGV.Rows)
    {
        if (!row.IsNewRow)
        {
        kon2.Open();    
        comm.CommandText = "SELECT WSID, Nominal,ROW_NUMBER() over (order by WSID) FROM [Data$] WHERE [WSID] = '"+ row.Cells["WSID"].Value +"'";
        row.HeaderCell.Value = (row.Index + 1).ToString();
        listBox1.Items.Add(); // I want to show row number of my WSID data that is shown in JadwalisiGV here
        kon2.Close();

        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer but I mean I want to show row number of my data that is store in database and shown in datargridview. For eample, my data 1234 is shown in row on of datagridview but it is stored in row five in my database. I want to show 5 to my listbox. Can you help me ?
@Alfonsus Dhani,You can try this way if you use oracle comm.CommandText = "SELECT WSID, Nominal,ROW_NUMBER() over (order by WSID) FROM [Data$] WHERE [WSID] = '"+ row.Cells["WSID"].Value +"'";
0

Try like this:

  foreach (DataGridViewRow row in JadwalisiGV.Rows)
    {.
     .
      int index = JadwalisiGV.Rows.IndexOf(row);
     .
     .
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.