Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
i cant bind data to dropdownlist in gridview from code behind without sqldatasource
 
my code is:
 
protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            GetData();
 
    }
    public void GetData()
    {
        DataTable dt = new DataTable();
    string strSql  =   "SELECT    Tbl_Order.ID_Order,"+
                                " Tbl_Order.FlNm, "   +
                                " Tbl_Order.Email,"   +
                                " Tbl_Order.Sbj,"     +
                                " Tbl_Order.PhNo,"    +
                                " Tbl_Order.Psg,"     +
                                " Tbl_Order.FlUpld,"  +
                                " Tbl_Order.Srl_No,"  +
                                " Tbl_Prst.pursuit"   +
                       " FROM  Tbl_Order INNER JOIN Tbl_Prst ON Tbl_Order._id_prst = Tbl_Prst.id_prst";
 
        Dal.Fill_Dt(strSql, dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataTable dt_pursuit = new DataTable();
        Dal.Fill_Dt("select _id_prst,pursuit from Tbl_Prst)", dt_pursuit);
 
        string FlNm = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFlNm")).Text;
        string Email = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmail")).Text;
        string Sbj = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSbj")).Text;
        string PhNo = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPhNo")).Text;
        string Psg = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPsg")).Text;
        string Srl_No = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSrl_No")).Text;
        string pursuit = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpursuit")).Text;
        string id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtID_Order")).Text;
 
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit;
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataTextField = "pursuit";
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataValueField= "_id_prst";
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataBind();
 
        string str = string.Format("update Tbl_Order set FlNm=N'{0}',Email=N'{1}',Sbj=N'{2}',PhNo=N'{3}',Psg=N'{4}',Srl_No=N'{5}'" +
                                   "WHERE ID_Order={6}", FlNm, Email, Sbj, PhNo, Psg, Srl_No, id);
        Dal.Cmd_Qry(str);
 
        GridView1.EditIndex = -1;
        GetData();
 
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
 
                <asp:GridView ID="GridView1"
                    AutoGenerateColumns="False"
                    OnRowUpdating="GridView1_RowUpdating"
                    OnRowEditing="GridView1_RowEditing"
                    OnRowCancelingEdit="GridView1_RowCancelingEdit"
                    OnRowDeleting="GridView1_RowDeleting"
                    DataKeyNames="ID_Order"
                    runat="server">
                    <Columns>
                        <asp:CommandField ShowEditButton="True" />
                       
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtID_Order" ReadOnly="true"  runat="server" Text='<%# Bind("ID_Order") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblID_Order" runat="server" Text='<%# Bind("ID_Order") %>'>></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtFlNm" runat="server" Text='<%# Bind("FlNm") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblFlNm" runat="server" Text='<%# Bind("FlNm") %>'>></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
 
.......
.........
............
 
 
i want bind data from code behind to dropdownlist like this
 
public void ShowDDL_pursuit()
    {
        DataTable dt_pursuit = new DataTable();
        Dal.Fill_Dt("select _id_prst,pursuit from Tbl_Prst)", dt_pursuit);
        DDLpursuit.DataSource = dt_pursuit;
        DDLpursuit.DataTextField = "pursuit";
        DDLpursuit.DataValueField = "_id_prst";
        DDLpursuit.DataBind();
 
    }
 
but is not unknown in class
and i try this:
 ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit;
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataTextField = "pursuit";
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataValueField= "_id_prst";
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataBind();
 
but it's not work
please help me how can i do this
Posted 14-Feb-13 22:38pm
jiji26631.8K
Comments
Gittu Dash at 15-Feb-13 4:21am
   
Could you plz specify what exact data do you want to bind with this DropDownList ?
jiji2663 at 15-Feb-13 4:48am
   
my database is like this:
http://picpaste.com/2-15-2013_1-10-23_PM-9g1MipC3.png
 
i want when user click on button "Edit" on Gridview inseted of TextBox, selected value from dropDownlist, like this:
http://picpaste.com/2-15-2013_1-15-36_PM-wVhwdet7.png
jiji2663 at 15-Feb-13 4:54am
   
i want create dropDownlist into gridviw like this:
http://picpaste.com/2-15-2013_1-22-51_PM-KwrpyXkK.png
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Add the dropdownList value at GridView1_RowDataBound event.
 

1. Add the grvDC_RowDataBound event to page
 
2. Get the dropdownlist value in dataset at pageload. and declare the DataTable 'dt_pursuit' as common to all function
 
3. In grvDC_RowDataBound event add the dataset value to dropdown

 
as
 
protected void grvDC_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
try {
ddl = new DropDownList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = (DropDownList)e.Row.FindControl("ddlDCTypes");
DDLpursuit.DataSource = dt_pursuit;
DDLpursuit.DataTextField = "pursuit";
DDLpursuit.DataValueField = "_id_prst";
 
}
 
} catch (Exception Ex) {
throw Ex;
}
}
  Permalink  
Comments
jiji2663 at 15-Feb-13 5:34am
   
i get error :
Object reference not set to an instance of an object.
in line:
 
ddl = (DropDownList)e.Row.FindControl("ddlDCTypes");
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
Have you tried something like this.
 
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit.Rows.AsQueryable().OfType().ToList();
 
I am not sure whether will work but could be worth a try.
 
Regards
Jegan
  Permalink  
Comments
jiji2663 at 15-Feb-13 5:42am
   
tnx
but isn't work
Jegan Thiyagesan at 15-Feb-13 6:45am
   
Have you tried something like:
 
((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit.DefaultView;
 
Regards
Jegan
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi
Actually answer is here:
 
bind data to dropdownlist with c#[^]
 
Regards
Jegan
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100