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 7911929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:26:04+00:00 2026-06-03T13:26:04+00:00

I’m using C# and Interop.Excel.Range to return a the used range of columns for

  • 0

I’m using C# and Interop.Excel.Range to return a the used range of columns for a worksheet and write the used columns and rows to a datagrid view. Some of the columns are returning blank/null.

Microsoft.Office.Interop.Excel.Range excelRange = wWorksheet.UsedRange;

TabPage wTabPage = new TabPage(wWorksheet.Name.ToString());
DataGridView wDGV = new DataGridView();
wDGV.Dock = DockStyle.Fill;
wTabPage.Controls.Add(wDGV);
Sheets_TabControl.TabPages.Add(wTabPage);

DataTable dt = new DataTable();
DataRow wNewRow = null;

for (int i = 0; i < excelRange.Columns.Count; i++)
{
    dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
}

string wValue = string.Empty;
Microsoft.Office.Interop.Excel.Range wRange = null;

for (int wRowIndex = 1; wRowIndex <= skipRows; wRowIndex++)
{
    wNewRow = dt.NewRow();

    foreach (DataColumn wColumn in dt.Columns)
    {
        wRange = excelRange.Cells[wRowIndex, wColumn.Ordinal + 1];

        if (wRange != null)
        {
            if (wRange.Value2 != null)
            {
                wValue = wRange.Value2.ToString();

                if (!string.IsNullOrEmpty(wValue))
                {
                    wNewRow.SetField(wColumn, wValue);
                }
            }

        }
    }

    dt.Rows.Add(wNewRow)
}

wDGV.DataSource = dt;

Is there a way to skip or ignore any columns that are blank, while writing those columns that contain data?

Thanks for the help!

  • 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-03T13:26:06+00:00Added an answer on June 3, 2026 at 1:26 pm

    The key to this is to use the VBA worksheet function CountA which returns a value indicating how many cells in a given range have data. Based on this you can decide whether or not to create a column in your DataTable.

    Since your table columns and Excel range columns are now out of sync, you’ll need to use the column name you set in your code as the index of the corresponding Excel range column.

    So, rejigging your code gives:

    Microsoft.Office.Interop.Excel.Range excelRange = wWorksheet.UsedRange;
    
    TabPage wTabPage = new TabPage(wWorksheet.Name.ToString());
    DataGridView wDGV = new DataGridView();
    wDGV.Dock = DockStyle.Fill;
    wTabPage.Controls.Add(wDGV);
    Sheets_TabControl.TabPages.Add(wTabPage);
    
    DataTable dt = new DataTable();
    DataRow wNewRow = null;
    
    // New code to create DataTable columns
    
    for (int i = 1; i <= excelRange.Columns.Count; i++)
    {
        // If the current column of cells does not contain any data, then don't add a column to the datatable
    
        if (xlSpreadsheet.App.WorksheetFunction.CountA(excelRange.Cells[Missing.Value, i]) != 0)
        {
            dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
        }
    }
    
    string wValue = string.Empty;
    Microsoft.Office.Interop.Excel.Range wRange = null;
    
    for (int wRowIndex = 1; wRowIndex <= excelRange.Rows.Count; wRowIndex++)
    {
        wNewRow = dt.NewRow();
    
        foreach (DataColumn wColumn in dt.Columns)
        {
            // Parse the column number we stored earlier as the column name
    
            int colNumber = 0;
    
            if (int.TryParse(wColumn.ColumnName, out colNumber))
            {
                // We use the parsed column number to index the column in the Excel range
    
                wRange = excelRange.Cells[wRowIndex, colNumber];
    
                if (wRange != null)
                {
                    if (wRange.Value2 != null)
                    {
                        wValue = wRange.Value2.ToString();
    
                        if (!string.IsNullOrEmpty(wValue))
                        {
                            wNewRow.SetField(wColumn, wValue);
                        }
                    }
                }
            }
        }
    
        dt.Rows.Add(wNewRow)
    }
    
    wDGV.DataSource = dt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build

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.