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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:54:48+00:00 2026-06-05T13:54:48+00:00

I’m trying to make a program that deletes columns in a DataSet that is

  • 0

I’m trying to make a program that deletes columns in a DataSet that is filled by an excel file. The way it deletes columns is it compares the header in each column with the first element in each row and deletes the column of any string that does not appear in the rows. My problem is that I’m getting a weird error that I can’t understand. it says:

The invocation of the constructor on type ‘Excel_Retriever.MainWindow’ that matches the specified binding constraints threw an exception.’ Line number ‘3’ and line position ‘9’.

I’m new to C# and XAML and would really appreciate any help with solving this error. Thank you! Here is my code:

XAML:

<Window x:Class="Excel_Retriever.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="ExcelGrid">
        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True" Height="289"        HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="481" />
    </Grid>
</Window>

C#:

namespace Excel_Retriever
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataSet excel = GetDataTableFromExcel("C:\\Users\\Sweet Lou\\Desktop\\Adjusted research info.xlsx", "Research");
            //dataGrid1.DataContext = excel.Tables[0];
            DataSet ignoreds = Ignore_Names(excel);
            dataGrid1.DataContext = ignoreds.Tables[0];
        }

        public DataSet GetDataTableFromExcel(string FilePath, string strTableName)
        {
            try
            {
                OleDbConnection con = new OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + "; Extended Properties=\"Excel 12.0;HDR=YES;\"");
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }

        public DataSet Ignore_Names(DataSet sheet)
        {
            DataSet ignoreds = sheet;
            DataColumn columnNames = sheet.Tables[0].Columns["Name"]; //first column with names
            //ignoreds.Tables[0].Columns.Add(columnNames);
            int j = 1;
            for (int i = 0; i < 15; i++) //change 15 to variable
            {
                while (String.Compare(columnNames.Table.Rows[i].ToString(), sheet.Tables[0].Columns[j].ColumnName, true) != 0)
                {
                    ignoreds.Tables[0].Columns.RemoveAt(j);
                    j++;
                }
                j++;
            }
            return ignoreds;
        }
    }
}
  • 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-05T13:54:49+00:00Added an answer on June 5, 2026 at 1:54 pm

    You don’t need to pass strTableName to your method as you never use it.

    If you Use @”” for strings you don’t need to escape things: @”c:\users….”;

    You’re getting an exception because you’re trying to nuke rows that don’t really exist. This is what your method should look like, if I understand your goal here correctly.

        public static void Ignore_Names(DataSet sheet) {
            var table = sheet.Tables[0];
            var columns = table.Columns;
            var nameColumn = columns["Name"];
    
            var names = new List<string>();
            foreach (DataRow row in nameColumn.Table.Rows)
                names.Add(row[0].ToString().ToLower());
    
            // Work from right to left.  If you delete column 3, is column 4 now 3, or still 4?  This fixes that issue.
            for (int i = columns.Count - 1; i >= 0; i--)
                if (!names.Contains(columns[i].ColumnName.ToLower()))
                    columns.RemoveAt(i);
        }
    

    Also, in MainWindow constructor, you should just do this after you set excel:

       Ignore_Names(excel);       
       dataGrid1.ItemsSource = excel.Tables[0].DefaultView; 
    

    Note that I’m setting ItemsSource, not DataContext, and I’m passing the DefaultView. You can remove your ItemsSource binding from the XAML entirely.

    You should really be using VSTO instead of DataSets, but that’s yet another thing to learn 🙂

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.