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

Binding RadGrid

3 Answers 221 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 07 Oct 2010, 05:12 AM
Hey,

I am new with telerik controls,and can't understand procedure required for binding a RadGrid.I've made a grid,this is .aspx file--

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
    GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView>
        <RowIndicatorColumn>
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px" />
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Items" UniqueName="ItemColumn">
                <ItemTemplate>
                    <asp:Label ID="lblItemName" runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlItems" runat="server" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ddlItems_SelectedIndexChanged" />
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Rate" UniqueName="RateColumn">
                <ItemTemplate>
                <asp:Label ID="lblRate" runat="server" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Quantity" UniqueName="QuantityColumn">
                <ItemTemplate>
                    <asp:Label ID="lblQuantity" runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadTextBox ID="txtQuantity" runat="server" />
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Amount" UniqueName="AmountColumn">
                <ItemTemplate>
                    <asp:Label ID="lblAmount" runat="server" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridButtonColumn HeaderText="Done" Text="Done" UniqueName="Donecolumn">
            </telerik:GridButtonColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

I want the grid to be in edited mode initially on the page load,such that it contains a row in which 1st column has dropdownlist from which person selects the item and next column contains a label which will get the rate of the selected item in ddl on selectedIndex Changed of ddl.I tried it with Item bound event like--.cs file
protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlItems.SelectedIndex > 0)
            {
                DataSet ds = objSQLHelper.GetProductDetails("select * from tblProducts where Pid=" + ddlItems.SelectedValue);
                if (ds != null)
                {
                    lblRate.Text = ds.Tables[0].Rows[0]["Rate"].ToString();
                }
            }
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
            {
                GridEditFormItem editform = (GridEditFormItem)e.Item;
                DropDownList ddlItems = (DropDownList)editform.FindControl("ddlItems");
                ddlItems.DataSource = ds;
                ddlItems.DataTextField = "ProductName";
                ddlItems.DataValueField = "Pid";
                ddlItems.DataBind();
            }
        }
 but that did'nt worked.I just want to show an empty edited RadGrid Initially At page load,where person places order by selecting values fom ddl and placing quantity in textbox.It not showing Grid in the browser.What to do?..And What's required thet i'm missing.Need some help...

Thanks
Amit.

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 07 Oct 2010, 08:56 AM
Hello Amit,

Since the Label control is inside ItemTemplate, access the dataitem first and then get the label. Check out the following code snippet.

C#:
protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList ddlItems = (DropDownList)sender;
       GridEditFormItem editItem = (GridEditFormItem)ddlItems.NamingContainer;
       GridDataItem item = (GridDataItem)editItem.ParentItem; // access GridDataItem
       Label lblRate = (Label)item.FindControl("lblRate"); // access Label using DataItem
       if (ddlItems.SelectedIndex > 0)
       {
          DataSet ds = objSQLHelper.GetProductDetails("select * from tblProducts where Pid=" + ddlItems.SelectedValue);
               if (ds != null)
               {
                   lblRate.Text = ds.Tables[0].Rows[0]["Rate"].ToString();
               }
       }
   }

Note: Set AutoPostBack="true" for DropDownList.

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 07 Oct 2010, 09:04 AM
But Why is grid not visible?...
I don't have any data within it,cuz i want users to add data to the grid,is that the reason?...
0
Accepted
Radoslav
Telerik team
answered on 13 Oct 2010, 10:51 AM
Hello Amit,

RadGrid is a component that displays data obtained from a database or other data source. The data is presented in tabular view.
There are three ways to bind the grid to a database:
  • Using the NeedDataSource event - RadGrid calls this event each time it needs a data source.
  •  DataGrid-like binding - You can set the DataSource property manually in the control declaration. This property specifies the database, which is then used as a source for the grid.
If the RadGrid is not bound it does not render anything on the page.

To achieve the desired functionality and show the RadGrid with is bound to empty data source, you could create a empty DataTable and bound the grid to it. To show the RadGrid in Edit mode initially you could try using the approach explained into following resource:
http://www.telerik.com/help/aspnet-ajax/grddefaulteditmodeforgriditemsoninitialload.html

I hope this helps.

Regards,
Radoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Amit
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or