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

How to Binding data to GridDropDownColumn from DataBase

3 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dung
Top achievements
Rank 1
Dung asked on 22 Sep 2015, 09:46 AM

Help Me!

VS2010, RadControls for ASP.NET AJAX Q3 2010. FrameWork 4.0
- How to Binding data to GridDropDownColumn from DataBase
- How to Insert/Update Value for Ragird1
 Thank !

 ASPX File

<%@ Page Language="C#" AutoEventWireup="true" validateRequest="false" enableEventValidation="false" CodeFile="HuyenThi_Datao.aspx.cs" Inherits="HuyenThi_Datao" MasterPageFile="~/AdminMasterPage.master" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>

<asp:Content ID="ContentHuyenThi_Datao" ContentPlaceHolderID="ContentPlaceHolder" runat="server">

    <!-- content begin -->


    <div class="qsf_content_area_wrapper">
        <div class="qsf_content_area">

                <table cellspacing="0" cellpadding="0" border="0" 
                    style="width: 95%; height: 18px;">

                  <tr>
                  <td style="width: 350px">
                  </td>
                     <td >
                        <asp:CheckBox ID="CheckBox2"  Text="Chọn tất cả các trang" runat="server" Height="22px">
                       </asp:CheckBox>
                    </td>
                    <td style="width: 120px">
                       <asp:Button ID="Button1" CssClass="button" Width="110px" Text="Export to Excel" OnClick="Button1_Click"
                          runat="server" Height="22px">
                      </asp:Button>
                    </td>
                     <td style="width: 120px">
                      <asp:Button ID="Button2" CssClass="button" Width="110px" Text="Export to Word" OnClick="Button2_Click"
                        runat="server" Height="22px">
                     </asp:Button>
                    </td>


                    <td style="width: 120px">
                       <asp:Button ID="Button3" CssClass="button" Width="110px" Text="Export to CSV" OnClick="Button3_Click"
                        runat="server" Height="22px">
                       </asp:Button>
                   </td>

                     <td style= "width:120px" >
                      <telerik:RadButton ID="RadButton3" runat="server" Width="110px" PostBackUrl="~/HuyenThi.aspx" 
                        Text="Tạo Mới" Height="22px">
                       </telerik:RadButton>
                      
                    </td>
                  </tr>
                </table>
             
    <br />

    <script type="text/javascript">
        function onRequestStart(sender, args) {
            if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ||
                    args.get_eventTarget().indexOf("ExportToWordButton") >= 0 ||
                    args.get_eventTarget().indexOf("ExportToCsvButton") >= 0) {
                args.set_enableAjax(false);
            }
        }
    </script>


            <table cellspacing="0" cellpadding="0" border="0"  width="100%">

                <tr >

                    <td >
                        <telerik:RadButton ID="RadButton2" runat="server" Font-Bold="True" 
                            Font-Names="Times New Roman" Font-Size="XX-Large" ForeColor="#0066FF" ReadOnly="True" 
                            Text=" Huyen Thi created" Height="20px" Width="98%" 
                            EnableBrowserButtonStyle="True">
                        </telerik:RadButton>
                        
                    </td>
                </tr>
            </table>

    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
     <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadGrid2">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid2"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
             <telerik:RadGrid Width="98%" runat="server" ID="RadGrid1" AllowPaging="True" 
                AllowSorting="True" OnDeleteCommand = "RadGrid1_DeletedCommand" 
                OnInsertCommand = "RadGrid1_InsertCommand" OnUpdateCommand= "RadGrid1_UpdateCommand"
            MasterTableView-NoMasterRecordsText="No Recorde" 
                OnNeedDataSource="RadGrid1_NeedDataSource" 
                OnSortCommand="RadGrid1_SortCommand"  
                OnPageIndexChanged="RadGrid1_PageIndexChanged" 
                OnPageSizeChanged="RadGrid1_PageSizeChanged" EnableViewState="False" 
                GridLines="None" Skin="Web20" AllowFilteringByColumn="True" PageSize="15" 
                    CellSpacing="-1">
                         
                    <MasterTableView DataKeyNames="HuyenThiId" AutoGenerateColumns="False"  EditMode="EditForms"
                    Width="100%" AdditionalDataFieldNames="HuyenThiId" AllowNaturalSort="False" 
                    EnableViewState="False" Font-Bold="False" Font-Italic="False" 
                        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                        GridLines="None">
                         <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                         <Columns>
                          <telerik:GridEditCommandColumn HeaderText="UpDate" EditText="UpDate">
                          </telerik:GridEditCommandColumn>
                             <telerik:GridDropDownColumn UniqueName="TinhTen"  ListDataMember="TinhTen" SortExpression="TinhTen" ListTextField="TinhTen" EnableEmptyListItem="true"
                              EmptyListItemText="--Chọn Tỉnh--" EmptyListItemValue="" ListValueField="TinhId"  HeaderText="Tên Tỉnh" DataField="TinhId" >
                             </telerik:GridDropDownColumn>
                        <telerik:GridBoundColumn DataField="HuyenThiId" HeaderText="ID HuyenThiId" ReadOnly="True" SortExpression="HuyenThiId" 
                            UniqueName="HuyenThiId" Visible="false">
                            <HeaderStyle Width="20px" ForeColor="Silver" />
                            <ItemStyle ForeColor="Silver" />
                         </telerik:GridBoundColumn>
                       
                        <telerik:GridBoundColumn DataField="HuyenThiTen" HeaderText="Ten Huyen Thi" SortExpression="HuyenThiTen"
                            UniqueName="HuyenThiTen" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="HuyenThiMoTa" HeaderText="Mô Tả" SortExpression="HuyenThiMoTa"
                            UniqueName="HuyenThiMoTa" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false">
                        </telerik:GridBoundColumn>

                        <telerik:GridHyperLinkColumn DataNavigateUrlFields="HuyenThiId" HeaderText="EDIT" DataNavigateUrlFormatString="~/HuyenThi.aspx?Id={0}" 
                            Text="<img alt='EDIT' src='images\Edit.gif' border='0' />" 
                                 UniqueName= "SuaColumn" AllowFiltering="False"> 
                            <HeaderStyle HorizontalAlign="center" />
                            <ItemStyle HorizontalAlign="Center" /> 
                        </telerik:GridHyperLinkColumn>
                        <telerik:GridButtonColumn CommandName="Delete" HeaderText="Del" ConfirmText=" Del Huyen Thi " Text="<img alt='Del' src='images\Delete.gif' border='0' />" UniqueName= "DeleteColumn" >
                            <HeaderStyle HorizontalAlign="center" />
                            <ItemStyle HorizontalAlign="Center" />
                        </telerik:GridButtonColumn>
                      </Columns>



                        <PagerStyle AlwaysVisible="True" />



                     </MasterTableView>



                    <FilterMenu>
                        <CollapseAnimation Type="None" />
                    </FilterMenu>


<HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Web20"></HeaderContextMenu>

           </telerik:RadGrid>

        </div>
    </div>
</asp:Content>

 ASPX.CS File

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bussiness;
using Model;
using Telerik.Web.UI;

public partial class HuyenThi_Datao : System.Web.UI.Page
{

    
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);

    }

    //Load data
    protected void LoadData()
    {
        RadGrid1.DataSource = ListHuyenThiService.SelectHuyenThiInTinhAll();
        RadGrid1.DataBind();
        
         RadGrid1.MasterTableView.GetColumn("HuyenThiTen").Display = true;
         RadGrid1.MasterTableView.GetColumn("DeleteColumn").Display = true;
         RadGrid1.MasterTableView.GetColumn("SuaColumn").Display = true;
    }



    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = ListHuyenThiService.SelectHuyenThiInTinhAll();

    }

    protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

        // How to get Value to Update Database
    }

    protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        // How to get Value to Update Database
    }


    private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
        {
            
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;

            GridDropDownListColumnEditor editor = editMan.GetColumnEditor("TinhTen") as GridDropDownListColumnEditor;
            editor.DataSource = ListTinhService.SelectTinhAll();
            editor.DataBind();

        }
    }

    protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
    {
        LoadData();
    }

    protected void RadGrid1_PageIndexChanged(object sender, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        LoadData();
    }

    protected void RadGrid1_SortCommand(object sender, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        LoadData();
    }



    protected void RadGrid1_DeletedCommand (object source, Telerik.Web.UI.GridCommandEventArgs e)

    {
        //Get the GridDataItem of the RadGrid     
        GridDataItem item = (GridDataItem)e.Item;
        //Get the primary key value using the DataKeyValue.     
        int intHuyenThiID = Convert.ToInt32(item.OwnerTableView.DataKeyValues[item.ItemIndex]["HuyenThiId"]);
        try
        {
            HuyenThiService.DeleteHuyenThi(intHuyenThiID);

            //Insert Log
            string strDes = "Xóa Huyện Thị ID=" + intHuyenThiID;
            string strIP = HttpContext.Current.Request.UserHostAddress;
            LogService.CreateLog(Page.User.Identity.Name, strDes, DateTime.Now, strIP);
            LoadData();
        }
        catch (Exception ex)
        {
            RadGrid1.Controls.Add(new LiteralControl("Không xóa được Nhóm này."));
            e.Canceled = true;
        }
           
    }

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ConfigureExport();
        RadGrid1.MasterTableView.ExportToExcel();
    }

    protected void Button2_Click(object sender, System.EventArgs e)
    {
        ConfigureExport();
        RadGrid1.MasterTableView.ExportToWord();
    }

    protected void Button3_Click(object sender, System.EventArgs e)
    {
        ConfigureExport();
        RadGrid1.MasterTableView.ExportToCSV();
    }

    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
        {
            ConfigureExport();
        }
    }

    public void ConfigureExport()
    {
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = CheckBox2.Checked;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.ExportSettings.FileName = "HuyenThi_Export_" + DateTime.Now.ToString("dd.MM.yyyy");
        RadGrid1.ExportSettings.HideStructureColumns = true;
    }

}

 

 

3 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 24 Sep 2015, 02:44 PM
Hi,

Try the following code.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
 {
   GridEditableItem item = (GridEditableItem)e.Item;
   DropDownList combo = (DropDownList)item["Gubun"].Controls[0];
   combo.DataSourceID = "SqlDataSource1";
}
}

You can find some useful techniques from the following documentation.
Customize/Configure GridDropDownColumn

Regards,
Maria Ilieva
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Dung
Top achievements
Rank 1
answered on 06 Oct 2015, 10:09 AM

Hi Maria Ilieva

I've been following your form but GridDropDownColumn not get data from the database (SQL Server2012) in Edit mode / Insert. Expect you to help. thanks a lot​

0
Maria Ilieva
Telerik team
answered on 06 Oct 2015, 10:28 AM
Hi,

Please make sure that you also set ListTextField and ListValueField for the GridDropDownColumn.

Regards,
Maria Ilieva
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Dung
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Dung
Top achievements
Rank 1
Share this question
or