Hi Team
I need help and want to improve my back end logic, when debugging its not displaying the data to the grid.
C#
public void PopulateGrid()
{
Managers.BatchMaterialManager.SetAdriotConnection();
var dtBatchRecipe = Managers.BatchMaterialManager.GetBatcherMatAndWbMat();
DataView dv = dtBatchRecipe.DefaultView;
Managers.BatchMaterialManager.SetDefaultConnection();
/*try
{
dv.RowFilter = string.Format("CONVERT({0}, System.String) not like %PREMIX%");
View.GridMacroRecipe.ItemsSource = dv;
DataView dvPremix = dtBatchRecipe.DefaultView;
dvPremix.RowFilter = string.Format("CONVERT({0}, System.String) like %PREMIX");
}catch(Exception ex)
{
}
*/
}
//user load control
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
LoadControl();
presenter.PopulateGrid();
}
// loads data to the server
public void LoadControl()
{
Managers.BatchMaterialManager.SetAdriotConnection();
presenter.PopulateComboBox();
Properties.Settings.Default.Process = "Recipes";
string[] arrayOil = RecipeName.Split('/');
string OilIndicator = string.Empty;
if (arrayOil.Length > 1)
{
OilIndicator = arrayOil[arrayOil.Length - 2].ToString();
}
else
{
// Handle the case where there is no '/' in RecipeName
OilIndicator = ""; // Or set it to some default value or throw an exception
}
string LastDigit = string.Empty;
if (RecipeName.Length > 1)
{
LastDigit = RecipeName[RecipeName.Length - 2].ToString();
}
else
{
// Handle the case where RecipeName is too short
LastDigit = ""; // Or set it to some default value or throw an exception
}
if (OilIndicator == "2")
{
txtBagWeight.IsEnabled = false;
}
}
// front end wpf grid
Dharmeshwaran SRank #819 · 2.8K reputationPosted Jun 22, 2024, 3:41 AM EDT
You should follow the below steps to correctly the show the data from any backend in WPF application. I just mocked the data to show the example
Step 1: Create the Backend Data Source
Create a simple data source in the form of a list of objects. In a real application, this could be fetched from a database or a web API.
//Model namespace WpfApp.Models { public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } }Step 2: Create the ViewModel
The ViewModel will hold the collection of products and provide it to the view.
// ViewModels/ProductViewModel.cs using System.Collections.ObjectModel; using WpfApp.Models; namespace WpfApp.ViewModels { public class ProductViewModel { public ObservableCollection Products { get; set; } public ProductViewModel() { // Ideally, you would fetch this data from a database or web service. Products = new ObservableCollection { new Product { Id = 1, Name = "Product 1", Price = 10.00m }, new Product { Id = 2, Name = "Product 2", Price = 20.00m }, new Product { Id = 3, Name = "Product 3", Price = 30.00m } }; } } }Step 3: Set Up the View (XAML)
Create the XAML file for your main window and bind the DataGrid to the ViewModel.
Step 4: Bind the ViewModel to the View
In the code-behind of the MainWindow, set the DataContext to an instance of the ViewModel.
// MainWindow.xaml.cs using System.Windows; using WpfApp.ViewModels; namespace WpfApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new ProductViewModel(); } } }Step 5: Build and Run the Application
Compile and run your application. The DataGrid should display the products defined in the ViewModel.
Note that this setup provides only a basic but complete example of displaying data in a DataGrid in WPF using a ViewModel and data binding. You need to alter the code based on your requirment