Is it possible to add a ComboBox with unique sets of data to each cell of a giving grid, using...

  • Thread starter Thread starter patmcfads14
  • Start date Start date
P

patmcfads14

Guest
Sorry if this does not make sense, but I am new to C# and WPF...

I have a case where I need to have a potentially different set of data for any giving cell of a grid...

In the example code I show below, I tried to create a DataTable with a column that is a typeof(ComboBox) and then create and add a ComboBox to the item.

Here is my code:



private void btnFill_Click(object sender, RoutedEventArgs e)
{
// Create a new data table
DataTable dt = new DataTable();
// Create and add the columns
DataColumn c1 = new DataColumn("UserId", typeof(ComboBox));
DataColumn c2 = new DataColumn("Age", typeof(Int32));
DataColumn c3 = new DataColumn("BirthDay", typeof(DateTime));
dt.Columns.Add(c1);
dt.Columns.Add(c2);
dt.Columns.Add(c3);

ComboBox cbo1 = new ComboBox();

cbo1.Items.Add("One");
cbo1.Items.Add("Two");
cbo1.Items.Add("Three");
cbo1.SelectedIndex = 0;

// Add your data - one row at a time
DataRow newRow = dt.NewRow();
newRow["UserId"] = cbo1;
newRow["Age"] = 24;
newRow["BirthDay"] = DateTime.Today.AddYears(-24);

cbo1.Items.Clear();
cbo1.Items.Add("Four");
cbo1.Items.Add("Five");
cbo1.Items.Add("Six");
cbo1.SelectedIndex = 0;

newRow = dt.NewRow();
newRow["UserId"] = cbo1;
newRow["Age"] = 74;
newRow["BirthDay"] = DateTime.Today.AddYears(-74);
dt.Rows.Add(newRow);

// Turn off auto column generation
grdData.AutoGenerateColumns = true;

// Clear current columns
grdData.Columns.Clear();

// Bind the grid
grdData.ItemsSource = dt.DefaultView;

}

The program is executed without any errors, but I just get the object string in the cell I expected to see the combo box:



I have found many examples of people using DataGridComboBoxColumn() object for setting the entire column equal to a combo Box with specific set of inputs, but in my case, the inputs may vary. So I could not figure out how to work that object into my logic.

Thanks in advance for any help that can be provided!!



Regards,

Patrick

Continue reading...
 
Back
Top