Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:30:52+00:00 2026-06-12T02:30:52+00:00

Help me with my code, because i don’t now how to do this simple

  • 0

Help me with my code, because i don’t now how to do this simple task. How to select 10 random rows from excel file?

        string filepath = @"C:\1.xlsx";
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + @";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text""";
        OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
        DataSet ds = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(ds);
        showdata.DataSource = ds.Tables[0];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:30:53+00:00Added an answer on June 12, 2026 at 2:30 am

    If you create a DataGridView on your Windows Form called RandomExcelRows, create a button called button1 and put the following code inside the Click event handler for button1:

    private void button1_Click(object sender, EventArgs e)
    {
        Excel.Range[] rows = RandomRows(10, @"C:\test\whatever.xlsx");
    
        DataTable dt = new DataTable();
    
        bool ColumnsCreated = false;
    
        foreach(Excel.Range row in rows)
        {
            object[,] values = row.Value;
    
            int columnCount = values.Length;
    
            if(!ColumnsCreated)
            {
                for(int i = 0; i < columnCount; i++)
                {
                    DataColumn dc = new DataColumn(String.Format("Column {0}", i));
                    dt.Columns.Add(dc);
                    ColumnsCreated = true;
                }
            }
    
            DataRow dr = dt.NewRow();
    
            for (int i = 0; i < columnCount; i++)
            {
                dr[String.Format("Column {0}", i)] = values[1,i+1];
            }
    
            dt.Rows.Add(dr);
        }
    
        RandomExcelRows.DataSource = dt;
    }
    

    And then create a method underneath called RandomRows containing the following:

    private Excel.Range[] RandomRows(int randomRowsToGet, string worksheetLocation, int worksheetNumber = 1, int lowestRow = 0, int highestRow = 99)
    {
        Excel.Range[] rows = new Excel.Range[randomRowsToGet];
    
        Excel.Application excel = new Excel.Application();
        Excel.Workbook workbook = excel.Workbooks.Open(worksheetLocation);
        Excel.Worksheet worksheet = workbook.Worksheets[worksheetNumber];
    
        List<int> rowNumbers = new List<int>();
    
        bool allUniqueNumbers = false;
    
        Random random = new Random();
    
        while (!allUniqueNumbers)
        {
            int nextNumber = random.Next(lowestRow, highestRow);
    
            if (!rowNumbers.Contains(nextNumber))
                rowNumbers.Add(nextNumber);
    
            if (rowNumbers.Count == randomRowsToGet)
                allUniqueNumbers = true;
        }
    
        for (int i = 0; i < randomRowsToGet; i++)
        {
            rows[i] = worksheet.UsedRange.Rows[rowNumbers[i]];
        }
    
        Marshal.ReleaseComObject(excel);
    
        return rows;
    }
    

    The program will take a random number of rows (this amount is specified by you) from the Spreadsheet you specify and place these rows into your DataGridView.

    It’s pretty crude, and needs refactoring but it is the basis of what you need. You could also get the string containing the file location from a textbox instead of hardcoding this, the same goes for the number of rows you want, and the other parameters such as minimum and maximum rows.

    As well, you could use an OpenFileDialog to allow your user to browse to your excel file, etc etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody help with this code I just don't know where I am going
I need help converting this code from C# to Java. public static ulong GetChecksum(byte[]
I'm going to try and explain this without showing any code because I don't
I currently have this code but it leaks because I don't know how to
I don't want code, just need help finding where to start this project. I
can any one help me with code that subtract 1 from a digit stored
I haven't written any code yet because i don't know where to start. I
I'm dealing with a specific and simple case. I need some help, because I
this question probably wont be explained very well and that's because I don't really
Actually I don't know if this question has already been answered because I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.