question

KwebenaAcquah-9104 avatar image
0 Votes"
KwebenaAcquah-9104 asked KwebenaAcquah-9104 commented

how to allow null Column Cast on datagrid in wpf

Please am having a datagrid that grid that loads data directly from the database like this:

 public partial class MainWindow : Window
            {
            private DataTable dataTable;
            private DataTable resDataTable;
            public MainWindow()
            {
              InitializeComponent();
               dataTable = GetDataTable();
              dataGrid.ItemsSource = dataTable.DefaultView;
          }
          public DataTable GetDataTable() 
          {
              string connString = @"";
              using (SqlConnection sqlConnection = new SqlConnection(connString))
              {
                  sqlConnection.Open();
                  using (SqlCommand command = new SqlCommand("select * from scoretable",sqlConnection))
                  {
                      DataTable dataTable = new DataTable();
                      dataTable.Load(command.ExecuteReader());
                      return dataTable;
                  }
              }
          }

83826-11.jpg

every thing works very fine while using this code method below;

  public List<string> getRank(string fieldName) 
  {       
      List<string> positions = new List<string>();
      List<int> values = dataTable.AsEnumerable().Select(s => s.Field<int>(fieldName)).OrderByDescending(d=>d).ToList();
      for (int i = 0; i < dataTable.Rows.Count; i++)
      {
         int index = values.IndexOf(dataTable.Rows[i].Field<int>(fieldName));
          positions.Add(AddOrdinal(index + 1));
      }
      return positions;
  }

here is my button click event that calls the getRank Method Above;

 private void button_Click(object sender, RoutedEventArgs e)
      {
          resDataTable = new DataTable();
          resDataTable.Columns.Add("id", typeof(string));
          for (int i = 1; i < dataTable.Columns.Count; i++)
          {
              resDataTable.Columns.Add(dataTable.Columns[i].ColumnName, typeof(string));
          }
          List<string> mathRank = getRank("Math");
          List<string> scienceRank = getRank("Science");
          List<string> englishRank = getRank("English");
          List<string> historyRank = getRank("History");
                            
      }

QUESTION:

but when a column happens to be NULL without any value from the MAIN LOADING WINDOW above when i try to cast it again i get this error saying:

![83922-33.png][3]

i want to allow NULL within this LOADING WINDOW programatically within my code method here

   public List<string> getRank(string fieldName) 
      {       
          List<string> positions = new List<string>();
          List<int> values = dataTable.AsEnumerable().Select(s => s.Field<int>(fieldName)).OrderByDescending(d=>d).ToList();
          for (int i = 0; i < dataTable.Rows.Count; i++)
          {
             int index = values.IndexOf(dataTable.Rows[i].Field<int>(fieldName));
              positions.Add(AddOrdinal(index + 1));
          }
          return positions;
      }

but i cannot do this can someone teach me how to Fix this Error Please; (thanks)



dotnet-csharpwindows-wpf
11.jpg (22.7 KiB)
33.png (28.2 KiB)
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KwebenaAcquah-9104
How did you declare your class? Is a field created in a class allowed to be null?For example, public DateTime? DateOfBirth { get; set; } indicates that DateOfBirth field can be null. Did this work for you? If it does not, could you give me more details to make a demo to reproduce your error?

0 Votes 0 ·

please DaisyTian, Sir i have just updated my code please peruse through it and see what i am talking about; please try to help me sir(thanks in advance)

0 Votes 0 ·

@KwebenaAcquah-9104
Could you try to create your fieldName field like public int? fieldName{ get; set; } ,and use

 List<int> values = dataTable.AsEnumerable().Select(s => s.Field<int?>(fieldName)).OrderByDescending(d=>d).ToList();

to replace

 List<int> values = dataTable.AsEnumerable().Select(s => s.Field<int>(fieldName)).OrderByDescending(d=>d).ToList();

to check if it works for you?

0 Votes 0 ·
Show more comments

Help!!!!!!!!!!!!!!!!!!!!!!!!! please Help Me!!!!!!!!!!!!! please (prodigies)

0 Votes 0 ·

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered KwebenaAcquah-9104 commented

HI,
use this LinQ:

     List<int> values = dataTable.AsEnumerable().Select(s => (s[fieldName] == DBNull.Value) ? 0 : s.Field<int>(fieldName)).OrderByDescending(d => d).ToList();
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks very much PeterFleicher, your code seems to be working it out but here is the error i got not from your line of code but from within the code block so i think you can help resolve this error
HERE
85107-youy.png



please sir (thanks)

0 Votes 0 ·
youy.png (27.2 KiB)

Hi,
you can use the same approach:

     for (int i = 0; i < dataTable.Rows.Count; i++)
     {
       int index = values.IndexOf((dataTable.Rows[i][fieldName] == DBNull.Value) ? 0 : dataTable.Rows[i].Field<int>(fieldName));
       positions.Add(AddOrdinal(index + 1));
     }
0 Votes 0 ·

thanks prodigy

0 Votes 0 ·