ComponentOne GridView for ASP.NET WebForms
Task-Based Help / Binding the Grid to a Data Source / Runtime Databinding of Template Field
In This Topic
    Runtime Databinding of Template Field
    In This Topic

    This topic demonstrates how to bind template fields to a C1GridView at runtime.

    In this example, the C1GridView control is bound to the Customers table of C1Nwind.mdb database, and the field Country is attached to a C1TemplateField containing a C1ComboBox.

    In the Designer

    Complete the following steps:

    1. Right-click the C1GridView control and select Show Smart Tag from the context menu to bind it to the C1Nwind.mdb database, which is located by default in the samples directory. For detailed steps go to Step 1 of 3: Binding C1GridView to a DataSource.
    2. Add a template field to the Country field column and add C1ComboBox in the ItemTemplate section in the editor document. For detailed steps go to Adding Controls to a Column.

    In Source View

    To add a Template column in C1GridView, modify the <cc1:C1GridView ></cc1:C1GridView > tag as shown below:

    <cc1:C1GridView ID="C1GridView1" runat="server" AutogenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
                <Columns>
                    <cc1:C1BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID">
                    </cc1:C1BoundField>
                    <cc1:C1BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName">
                    </cc1:C1BoundField>
                      <cc1:C1TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <span>
                            <%# Eval("Country") %></span>
                    </ItemTemplate>
                    <EditItemTemplate>
                       <cc1:C1ComboBox ID="dlCountry" runat="server">
                       </cc1:C1ComboBox>
                     </EditItemTemplate>
                </cc1:C1TemplateField>
                <cc1:C1CommandField ShowEditButton="True">
                </cc1:C1CommandField>
            </Columns>
               
            </cc1:C1GridView>

    In Code

    1. To bind the C1ComboBox in each row, add the following code to handle the RowDataBound event :

      To write the code in Visual Basic

      Visual Basic
      Copy Code
       Protected Sub C1GridView1_RowDataBound(sender As Object, e As C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowEventArgs) Handles C1GridView1.RowDataBound
              If (e.Row.RowState And C1GridViewRowState.Edit) > 0 Then
                  ' Retrieve the underlying data item. In this example
                  ' the underlying data item is a DataRowView object.
                  Dim rowView As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
                  ' Retrieve the country value for the current row.
                  Dim country As [String] = rowView("Country").ToString()
                  ' Retrieve the DropDownList control from the current row and DataBind it.
                  Dim dlCountry As C1ComboBox = DirectCast(e.Row.FindControl("dlCountry"), C1ComboBox)
                  dlCountry.DataSource = GetData()
                  dlCountry.DataTextField = "Country"
                  dlCountry.DataValueField = "Country"
                  dlCountry.DataBind()
                  'Retrieve item from dropdown and set it as selected.
                  For Each item As C1ComboBoxItem In dlCountry.Items
                      If item.Text = country Then
                          dlCountry.SelectedValue = item.Text
                      End If
                  Next
              End If
          End Sub

          Protected Function GetData() As DataTable
              Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/App_Data/C1NWind1.mdb"))
              Dim cmd As New OleDbCommand("Select Country from Countries", con)
              Dim da As New OleDbDataAdapter(cmd)
              Dim dt As New DataTable()
              da.Fill(dt)
              Return dt
          End Function

      To write the code in C#

      C#
      Copy Code
      protected void C1GridView1_RowDataBound(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowEventArgs e)
          {
              if ((e.Row.RowState & C1GridViewRowState.Edit)>0) {
                  // Retrieve the underlying data item. In this example
                  // the underlying data item is a DataRowView object.
                  DataRowView rowView = (DataRowView)e.Row.DataItem;
                  // Retrieve the country value for the current row.
                  String country = rowView["Country"].ToString();
                  // Retrieve the DropDownList control from the current row and DataBind it.
                
                  C1ComboBox dlCountry = (C1ComboBox)e.Row.FindControl("dlCountry");
                  dlCountry.DataSource = GetData();
                  dlCountry.DataTextField = "Country";
                  dlCountry.DataValueField = "Country";
                  dlCountry.DataBind();
                 
                  //Retrieve item from dropdown and set it as selected.
                
                  foreach (C1ComboBoxItem item in dlCountry.Items) {
                      if (item.Text == country) {
                          dlCountry.SelectedValue = item.Text;
                      }
                  }
               }
            }
          DataTable GetData() {
              OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("~/App_Data/C1NWind1.mdb"));
              OleDbCommand cmd = new OleDbCommand("Select Country from Countries",con);
              OleDbDataAdapter da = new OleDbDataAdapter(cmd);
              DataTable dt = new DataTable();
              da.Fill(dt);
              return dt;
          }

    2. To provide the parameters, i.e. SelectedValue of C1ComboBox, to the Update command, add the following code to handle the RowUpdating event:

      To write the code in Visual Basic

      Visual Basic
      Copy Code
       Protected Sub C1GridView1_RowUpdating(sender As Object, e As C1.Web.Wijmo.Controls.C1GridView.C1GridViewUpdateEventArgs) Handles C1GridView1.RowUpdating
              ' Retrieve the row being edited.
              Dim row As C1GridViewRow = C1GridView1.Rows(C1GridView1.EditIndex)
              ' Retrieve the DropDownList control from the row.
              Dim combo As C1ComboBox = DirectCast(row.FindControl("dlCountry"), C1ComboBox)
              ' Add the selected value of the DropDownList control to
              ' the NewValues collection. The NewValues collection is
              ' passed to the data source control, which then updates the
              ' data source.
              e.NewValues("Country") = combo.SelectedValue
              SqlDataSource1.UpdateCommand = "Update Customers Set CustomerName=@CustomerName, Country=@Country where CustomerID=@CustomerID"
              SqlDataSource1.UpdateParameters.Add("CustomerID", e.Keys("CustomerID").ToString())
              SqlDataSource1.UpdateParameters.Add("CustomerName", e.NewValues("CustomerName").ToString())
              SqlDataSource1.UpdateParameters.Add("Country", e.NewValues("Country").ToString())
              SqlDataSource1.Update()
          End Sub

      To write the code in C#

      C#
      Copy Code
       protected void C1GridView1_RowUpdating(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewUpdateEventArgs e)
          {
              // Retrieve the row being edited.
              C1GridViewRow row = C1GridView1.Rows[C1GridView1.EditIndex];
              // Retrieve the DropDownList control from the row.
            
              C1ComboBox combo = (C1ComboBox)row.FindControl("dlCountry");
              // Add the selected value of the DropDownList control to
              // the NewValues collection. The NewValues collection is
              // passed to the data source control, which then updates the
              // data source.
            
              e.NewValues["Country"] = combo.SelectedValue;
              SqlDataSource1.UpdateCommand = "Update Customers Set CustomerName=@CustomerName, Country=@Country where CustomerID=@CustomerID";
              SqlDataSource1.UpdateParameters.Add("CustomerID", e.Keys["CustomerID"].ToString());
              SqlDataSource1.UpdateParameters.Add("CustomerName", e.NewValues["CustomerName"].ToString());
              SqlDataSource1.UpdateParameters.Add("Country", e.NewValues["Country"].ToString());
              SqlDataSource1.Update();
          }

    See Also

    Working with GridView for ASP.NET Web Forms