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

Personalised datasource(code-behind)

3 Answers 456 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Isabelle
Top achievements
Rank 1
Isabelle asked on 14 May 2014, 06:57 PM
Hi,

I start to work with Telerik RadGrid and i would like to know:

My datasource is an sql stored procedure and contain many fields that i don't want to show. So, is it possible to configure the RadGrid column by column?

In the same way, i need 6 textbox fields at the end of this grid, that user can enter an integer value.

It possible to include textbox in the RadGrid and when the user click save button, to get values programmaticaly in code-behind?

Thanks!

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 May 2014, 07:11 AM
Hi Isabelle,

You can take a look at the following article which will give you an understanding on using columns in RadGrid.
Column Types
Demo-Column Types

For your requirement you can use GridTemplateColumn. You can hide columns using the property Display. Please take a look at the following sample code snippet.

ASPX:
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="True" OnNeedDataSource="radGridOnNeedDataSource">
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="OrderID">
        <CommandItemTemplate>
            <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
        </CommandItemTemplate>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID"
                Display="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Value">
                <ItemTemplate>
                    <telerik:RadNumericTextBox ID="rntxtValue" runat="server">
                    </telerik:RadNumericTextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>  
</telerik:RadGrid>

C#:
protected void radGridOnNeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");      
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    DataTable myDataTable = new DataTable();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}
protected void btnSave_Click(object sender, EventArgs e)
{     
    foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
    {
        //Accessing Template Column
        RadNumericTextBox rntxtValue = (RadNumericTextBox)dataItem.FindControl("rntxtValue");
        string value = rntxtValue.Text;
        //Accessing datakeyvalue
        string ID = dataItem.GetDataKeyValue("OrderID").ToString();
        //Accessing BoundColumn value
        string city = dataItem["ShipCity"].Text;
        //Code to Save
    }
}

Thanks,
Princy
0
Isabelle
Top achievements
Rank 1
answered on 15 May 2014, 02:34 PM
Thanks for your fast reply!

It works great!

But how can i add dynamic textbox in code behind?   

boundColumn = New GridBoundColumn()
MyRadGrid.MasterTableView.Columns.Add(boundColumn)
boundColumn.UniqueName = "description"
boundColumn.DataField = "description"
 
Dim TextBox = New TextBox()
TextBox.ID = "XXS"
 
'HERE

When i try MyRadGrid.Controls.add(Textbox" i don't see nothing?
0
Princy
Top achievements
Rank 2
answered on 16 May 2014, 03:17 AM
Hi Isabelle,

You can create TextBox in the ItemTemplate of GridTemplateColumn dynamically. Take a look at the article which describes it in detail. Try and let me know if any concern.
Creating Template Columns Programmatically

Thanks,
Princy
Tags
Grid
Asked by
Isabelle
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Isabelle
Top achievements
Rank 1
Share this question
or