Monday, April 16, 2012

Binding Dropdown in Gridview



protected void grdInvoices_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable mytable = new DataTable();
        DataColumn productidcolumn = new DataColumn("SourceKey");
        DataColumn productnamecolumn = new DataColumn("SourceDescription");

        mytable.Columns.Add(productidcolumn);
        mytable.Columns.Add(productnamecolumn);

        DataSet ds = new DataSet();
        ds = DataBinder();
        int categoryid = 0;
        string expression = string.Empty;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //categoryid = Int32.Parse(e.Row.Cells[0].Text);
         
             //Get the DataKey value in case you set the DataKeyname in your GridView
             string key = grdInvoices.DataKeys[e.Row.RowIndex].Value.ToString();
             expression =key;
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlSourceDesc");
            DataRow[] rows = ds.Tables[0].Select();

            foreach (DataRow row in rows)
            {
                DataRow newrow = mytable.NewRow();
                newrow["SourceKey"] = row["SourceKey"];
                newrow["SourceDescription"] = row["SourceDescription"];
                mytable.Rows.Add(newrow);
            }
            ddl.DataSource = mytable;
            ddl.DataTextField = "SourceDescription";
            ddl.DataValueField = "SourceKey";
            ddl.DataBind();
        }
    }


 public DataSet DataBinder()
    {
        SystemProblems ObjSys = new SystemProblems();

        ObjSys.pClientId = clsSessions.sesClientId;
        DataSet ds = new DataSet();
        ds = ObjSys.BindData();
        return ds;

    }


 public DataSet BindData()
        {
            sqlParam = new SqlParameter[1];

            sqlParam[0] = new SqlParameter("@CID", SqlDbType.VarChar, 50);
            sqlParam[0].Value = _lClientId;

            objMgr = new Manager();
            DataSet dsClients;
            dsClients = objMgr.executeQuery("cs_GetSystemProblemSource", sqlParam, "tblSysProblem");
            return (dsClients);

        }

No comments:

Post a Comment