Hi,
I am having trouble adding a dropdown to DataGrid at runtime.
I create all boundColumns and the TemplateColumn for the dropdown at runtime.
The dropdown has a different datasource then the DataGrid.
1. If I try to bind the second datasource to the dropdown I get An unhandled exception of type 'System.StackOv erflowException ' occurred in mscorlib.dll.
2. If I comment that line out it displays always the dropdown. I want it
to display only in edit mode. And also since I add the dropdown in to the DataGrid I cannot
switch to edit mode. None of my other columns are editable.
this is what I have so far:
=============== =============== =============== =============== =============== ===
public class dgview1 : System.Web.UI.P age
{
//....
private void Page_Load(objec t sender, System.EventArg s e)
{
//....
AddSelectedColu mns();
BindGrid(defaul tOrderField);
}
private void AddSelectedColu mns()
{
BoundColumn bCol;
TemplateColumn tCol;
int i;
try
{
for (i=0; i < v_ColCode.Lengt h; i++)
{
if (v_ColCode[i] != "CompanyJobCode ")
{
bCol = new BoundColumn();
bCol.HeaderText = v_ColDesc[i];
bCol.DataField = v_ColCode[i];
bCol.SortExpres sion = v_ColCode[i];
if (v_ColCode[i] == "EmployeeID ")
bCol.ReadOnly = true;
_DataGrid.Colum ns.Add(bCol);
}
else
{
tCol = new TemplateColumn( );
tCol.HeaderText = v_ColDesc[i];
tCol.SortExpres sion = v_ColCode[i];
tCol.ItemTempla te = new CompanyJobCode( );
_DataGrid.Colum ns.Add(tCol);
}
}
}
catch (Exception ex)
{
lbl_debug.Text = lbl_debug.Text + "Exeption in function[getTotalCol()] : " + (ex.ToString()) + "<BR>";
}
}
private void BindGrid(string strOrderField)
{
SqlConnection conn = new SqlConnection(s trDBConn);
String strSqlQuery = "exec dbo.usp_GetEmpl oyeeDataByAll @OrderField = '" + strOrderField + "'";
SqlDataAdapter da = new SqlDataAdapter( strSqlQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds);
_DataGrid.DataS ource = ds.Tables[0];
_DataGrid.DataB ind();
}
}
public class CompanyJobCode : ITemplate
{
public CompanyJobCode( ) { }
public void InstantiateIn(C ontrol container)
{
DropDownList myDropDownList = new DropDownList();
myDropDownList. ID = "CompanyJobCode ";
myDropDownList. DataBinding += new EventHandler(th is.BindCompanyJ obCodeColumn);
container.Contr ols.Add(myDropD ownList);
}
public void BindCompanyJobC odeColumn(objec t sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(s trDBConn);
String strSqlQuery = "select * from tbl_CompanyJobC ode";
SqlDataAdapter da = new SqlDataAdapter( strSqlQuery, conn);
string SelectedCompany JobCode;
DropDownList dropdownlist = (DropDownList)s ender;
try
{
DataSet ds = new DataSet();
da.Fill(ds);
dropdownlist.Da taSource = ds.Tables[0];
dropdownlist.Da taTextField = "CompanyJobDesc ";
dropdownlist.Da taValueField = "CompanyJobCode ";
// dropdownlist.Da taBind();
DataGridItem container = (DataGridItem)d ropdownlist.Nam ingContainer;
SelectedCompany JobCode = Convert.ToStrin g(((DataRowView )container.Data Item)["CompanyJobCode "]);
if (dropdownlist.I tems.FindByValu e(SelectedCompa nyJobCode) != null)
dropdownlist.It ems.FindByValue (SelectedCompan yJobCode.ToStri ng()).Selected = true;
}
catch (Exception ex)
{
Console.WriteLi ne ("Error in BindCompanyJobC odeColumn" + ex.ToString());
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
=============== =============== =============== =============== =============== ========
So, I'm stuck.
- Kilic
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>pdlLKLZ8oU+ T62eYJhM2WQ==</Id>