0

I recently started this new project in XAML / C# using Visual Studio, now for some reason when I try to take user's input by using a Visual Basic InputBox so I can then use the thing the user typed as the name of the column it excecutes the functions but it does not display the newly created column. The worst thing is that there are no errors showing up in the IntelliSense error detector that Visual Studio offers.

Here is the part of the code that I used to Add and Remove the column (C#):

private void AddColumnButton_Click(object sender, RoutedEventArgs e)
{
    // Prompt the user for the column name
    string columnName = Microsoft.VisualBasic.Interaction.InputBox("Enter the name of the new column:", "Add Column");

    // Add the new column to the DataTable
    DataColumn newColumn = new DataColumn(columnName);
    table.Columns.Add(newColumn);

    // Set the DataTable's ItemsSource property to the new DataView
    DataView view = table.DefaultView;
    DataTable.ItemsSource = view;
}

private void RemoveColumnButton_Click(object sender, RoutedEventArgs e)
{
    if (table.Columns.Count != 3)
    {
        if (table.Columns.Count > 0)
        {
            table.Columns.RemoveAt(0);
            DataTable.ItemsSource = table.DefaultView;
        }
    }
}

And here is the entire code:

GithubRepository

I tried to change it to an approach where you type the name of the column in the same window, but it does not display it either.

EvoCorps
  • 19
  • 2
  • 1
    I think it's always a good idea to explain what exactly is not working. "but it doesn't work" is not helpful to understand what needs to get fixed. Regarding your posted code, you should check your DataTable instance handling. DataTable doesn't implement any change notifications. You must create a new instance of DataTable when you want to modify it. Or set ItemsSource to NULL before re-assigning the existing DataTable instance. – BionicCode Apr 15 '23 at 13:57
  • You can combine the conditions of the two if-statements with a logical AND: `if(table.Columns.Count != 3 && table.Columns.Count > 0)`. But this condition is a bit strange. Wouldn't simply testing `table.Columns.Count > 3` work if you need to keep at least 3 buttons? – Olivier Jacot-Descombes Apr 15 '23 at 14:03
  • Hi @BionicCode, you are totally right. I was in a bit of a rush, and i didn't explain the problem enough. I'm going to explain the problem more now. Thank you for your time. – EvoCorps Apr 15 '23 at 14:08
  • Note that the errors (red squiggles) and warnings (green squiggles) and suggestions (gray dotted lines) report only syntactical issues and possible problems resulting from a static analysis of your code. The compiler does not know what you try to achieve and cannot tell you that your code will not reach this goal. What happens at runtime is a completely different story. – Olivier Jacot-Descombes Apr 15 '23 at 14:16
  • @OlivierJacot-Descombes thank you, i didn't know it. – EvoCorps Apr 15 '23 at 14:21
  • See: [What is the difference between runtime and compile-time?](https://stackoverflow.com/q/15868456/880990). – Olivier Jacot-Descombes Apr 15 '23 at 14:22

0 Answers0