This is a migrated thread and some comments may be shown as answers.

GridBoundColumn DataField

2 Answers 1246 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 26 Aug 2019, 03:46 PM

Is it possible to change the DataField property of the GridBoundColumn based upon a selected value outside of the Grid?

 

thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Attila Antal
Telerik team
answered on 29 Aug 2019, 12:11 PM

Hi John,

Yes, it can be done. You will need to familiarize yourself with the Accessing Cells and Rows article to understand how to access items in the grid.

Here is a complete example where the DataField is being changed on a Button Click (same approach could be used for other control, OnSelectedIndexChanged events, etc..) 

<telerik:RadButton ID="RadButton1" runat="server" Text="Show Freight" OnClick="RadButton1_Click"></telerik:RadButton>

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn DataField="ShipCountry" HeaderText="Ship Country" UniqueName="MyColumn">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

 

C# - Code behind logic:

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = OrdersTable(); 
}

private DataTable OrdersTable()
{
    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
    dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
    dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
    dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));

    dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };

    for (int i = 0; i < 70; i++)
    {
        int index = i + 1;

        DataRow row = dt.NewRow();

        row["OrderID"] = index;
        row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
        row["Freight"] = index * 0.1 + index * 0.01;
        row["ShipName"] = "Name " + index;
        row["ShipCountry"] = "Country " + index;

        dt.Rows.Add(row);
    }

    return dt;
}


protected void RadButton1_Click(object sender, EventArgs e)
{
    RadButton btn = sender as RadButton;
    // access the column
    GridBoundColumn col = RadGrid1.MasterTableView.GetColumn("MyColumn") as GridBoundColumn;

    // if condition met
    if (btn.Text == "Show Freight")
    {
        btn.Text = "Show ShipCountry";
        // change the DataField
        col.DataField = "Freight";
        // optionally you can change the Column header text as well
        col.HeaderText = "Freight";
    }else
    {
        btn.Text = "Show Freight";
        // change the DataField
        col.DataField = "ShipCountry";
        // optionally you can change the Column header text as well
        col.HeaderText = "Ship Country";
    }

    // force Rebind() the grid for the changes to take effect
    RadGrid1.Rebind();
}


Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
John
Top achievements
Rank 1
answered on 29 Aug 2019, 02:30 PM

Thank You,

I was using the RadGrid1.MasterTableView.OwnerGrid.Columns collection.  Experienced a brain freeze on this one!!

 

thanks

Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
John
Top achievements
Rank 1
Share this question
or