Sameek Kundu
Sameek Kundu

Reputation: 147

Adding Combobox in a Datagrid cell dynamically in WPF

I am developing an application in WPF, and a part of the application involves pulling and merging data frfom three excel sheets and displaying in datagrid. Now if a row in datagrid has a queue that corresponds to more than one application , then it will display a combobox , whoseitems will be pulled from another excel sheets by looking up the correspoding queue name in that excel sheet.

I had before used the same functionality in ASP.NET and achieved quite easily , using the following code :

    for (int i = 0; i < dt.Rows.Count; i++)
  {
    //Filling datatable dsq from excel sheet
    //dsq contains the data where it is found out how many applications are there for               
   //ith row in Gridview

     count[i] = dsq.Tables[0].Rows.Count;

     foreach (GridViewRow row in gvApplications.Rows)
     {

     // Transferring gridview rows to a datatable
     if (row.Cells[0].Text.Trim().Equals(dt.Rows[i][0].ToString().Trim()))
       {
    //Dropdownlist is added only when no. of applications for each queue , i.e. , 
    /count[i] is greater than 1 , else level control is added.

         if (count[i] > 1)
            {
               DropDownList drp = new DropDownList();
               drp.DataSource = dsq.Tables[0];
                drp.DataTextField = "Application Name";
                drp.DataValueField = "Application Name";
                drp.DataBind();
               row.Cells[7].Controls.Add(drp);
             }
          }
           else
          {
             Label l = new Label();
            l.Text = dsq.Tables[0].Rows[0][0].ToString().Trim();
             l.ID = "label" + row.RowIndex.ToString();
            row.Cells[7].Controls.Add(l);

           }

                        }

Now I want the same functionality in WPf. Kindly help me out.

Upvotes: 0

Views: 614

Answers (1)

XAMeLi
XAMeLi

Reputation: 6289

You can use a DataGridTemplateColumn inside that you can put a ContentControl with DataTemplateSelector.

Upvotes: 2

Related Questions