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

Bind() from column not in DT

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Derek
Top achievements
Rank 1
Derek asked on 13 Nov 2020, 04:03 PM

Hi, I am trying to bind into a FormTemplate for editing from a GridBoundColumn 'value' where on ItemDataBound I pulled the information from multiple other columns in my Data Table (value_int, value_string, value_bit).  For each row only one column would have information and the rest are null. 

 

I haven't been able to figure out how to populate the textbox from 'value' let alone eventually write this back.  Another thing I have tried other than (unsuccessfully) getting the data from the 'value' column in my grid was to use bind() for each column in the data table as such:

<asp:TextBox ID="txtBoxValue" runat="server" Text='<%# Bind("Value_string") ?? Bind("value_decimal18_2") ?? Bind("value_bit") %>'></asp:TextBox>

 

But this only binds whatever value_bit shows as either true/false or a space as it doesn't seem to be a null.  code behind to bind the 'value' column is the following:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
 
            if (e.Item is GridDataItem && e.Item != null)
            {
                GridDataItem item = (GridDataItem)e.Item;
                DataRowView drv = (DataRowView)e.Item.DataItem;
 
                if (drv["value_type"] != null)
                {
                    if ((string)drv["value_type"] == "value_string")
                    {
                        item["Value"].Text = drv["value_string"].ToString();
                    }
                    else if ((string)drv["value_type"] == "value_bigint")
                    {
                        item["Value"].Text = drv["value_bigint"].ToString();
                    }
                    else if ((string)drv["value_type"] == "value_decimal18_2")
                    {
                        item["Value"].Text = drv["value_decimal18_2"].ToString();
                    }
                    else if ((string)drv["value_type"] == "value_bit")
                    {
                        item["Value"].Text = drv["value_bit"].ToString();
                    }
                }
}

 

 

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 18 Nov 2020, 03:53 PM

Hi Derek,

The logic you shared is not needed to bind values to the TextBoxes.

With RadGrid you will need to take care only of the data binding (Programmatic Data Binding Using the NeedDataSource Event), define the DataField for the column (GridBoundColumn)  and the rest will be taken care of by the Grid.

If you are using TemplateColumns, all you need to do is to use the Bind expressions Bind() and Eval().

Eval() is read-only and is used to display the data, Bind() is a two-way binding used to bind input value to the items and will be included in the Update process.

<telerik:GridTemplateColumn UniqueName="MyTemplateColumn">
    <ItemTemplate>
        <%--This template is for displaying the data--%>
        <%--Use Eval() expression to fetch the data from a specific field. Eval() will not allow two-way binding, if it is defined in a textbox, TextBox value will not be taken into account.--%>

        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>

        <%--This template will be rendered when clicking on the edit button--%>
        <%--Use Bind() expression for two-way binding. Anything entered in the textbox will be taken into account.--%>

        <telerik:RadTextBox ID="RadTextBox1" runat="server" Text='<%# Bind("ContatName") %>'></telerik:RadTextBox>

    </EditItemTemplate>
</telerik:GridTemplateColumn>

 

You may also check out the following articles:

 

Please check out the articles, try building a Grid and if you have difficulties at any point, let us know and we'll guide you further,.

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

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