K
Kilic
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.StackOverflowException' 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.Page
{
//....
private void Page_Load(object sender, System.EventArgs e)
{
//....
AddSelectedColumns();
BindGrid(defaultOrderField);
}
private void AddSelectedColumns()
{
BoundColumn bCol;
TemplateColumn tCol;
int i;
try
{
for (i=0; i < v_ColCode.Length; i++)
{
if (v_ColCode != "CompanyJobCode")
{
bCol = new BoundColumn();
bCol.HeaderText = v_ColDesc;
bCol.DataField = v_ColCode;
bCol.SortExpression = v_ColCode;
if (v_ColCode == "EmployeeID")
bCol.ReadOnly = true;
_DataGrid.Columns.Add(bCol);
}
else
{
tCol = new TemplateColumn();
tCol.HeaderText = v_ColDesc;
tCol.SortExpression = v_ColCode;
tCol.ItemTemplate = new CompanyJobCode();
_DataGrid.Columns.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(strDBConn);
String strSqlQuery = "exec dbo.usp_GetEmployeeDataByAll @OrderField = '"
+ strOrderField + "'";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds);
_DataGrid.DataSource = ds.Tables[0];
_DataGrid.DataBind();
}
}
public class CompanyJobCode : ITemplate
{
public CompanyJobCode() { }
public void InstantiateIn(Control container)
{
DropDownList myDropDownList = new DropDownList();
myDropDownList.ID = "CompanyJobCode";
myDropDownList.DataBinding += new
EventHandler(this.BindCompanyJobCodeColumn);
container.Controls.Add(myDropDownList);
}
public void BindCompanyJobCodeColumn(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strDBConn);
String strSqlQuery = "select * from tbl_CompanyJobCode";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
string SelectedCompanyJobCode;
DropDownList dropdownlist = (DropDownList)sender;
try
{
DataSet ds = new DataSet();
da.Fill(ds);
dropdownlist.DataSource = ds.Tables[0];
dropdownlist.DataTextField = "CompanyJobDesc";
dropdownlist.DataValueField = "CompanyJobCode";
// dropdownlist.DataBind();
DataGridItem container = (DataGridItem)dropdownlist.NamingContainer;
SelectedCompanyJobCode =
Convert.ToString(((DataRowView)container.DataItem)["CompanyJobCode"]);
if (dropdownlist.Items.FindByValue(SelectedCompanyJobCode) != null)
dropdownlist.Items.FindByValue(SelectedCompanyJobCode.ToString()).Selected =
true;
}
catch (Exception ex)
{
Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
============================================================================
=======
So, I'm stuck.
- Kilic
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.StackOverflowException' 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.Page
{
//....
private void Page_Load(object sender, System.EventArgs e)
{
//....
AddSelectedColumns();
BindGrid(defaultOrderField);
}
private void AddSelectedColumns()
{
BoundColumn bCol;
TemplateColumn tCol;
int i;
try
{
for (i=0; i < v_ColCode.Length; i++)
{
if (v_ColCode != "CompanyJobCode")
{
bCol = new BoundColumn();
bCol.HeaderText = v_ColDesc;
bCol.DataField = v_ColCode;
bCol.SortExpression = v_ColCode;
if (v_ColCode == "EmployeeID")
bCol.ReadOnly = true;
_DataGrid.Columns.Add(bCol);
}
else
{
tCol = new TemplateColumn();
tCol.HeaderText = v_ColDesc;
tCol.SortExpression = v_ColCode;
tCol.ItemTemplate = new CompanyJobCode();
_DataGrid.Columns.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(strDBConn);
String strSqlQuery = "exec dbo.usp_GetEmployeeDataByAll @OrderField = '"
+ strOrderField + "'";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds);
_DataGrid.DataSource = ds.Tables[0];
_DataGrid.DataBind();
}
}
public class CompanyJobCode : ITemplate
{
public CompanyJobCode() { }
public void InstantiateIn(Control container)
{
DropDownList myDropDownList = new DropDownList();
myDropDownList.ID = "CompanyJobCode";
myDropDownList.DataBinding += new
EventHandler(this.BindCompanyJobCodeColumn);
container.Controls.Add(myDropDownList);
}
public void BindCompanyJobCodeColumn(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strDBConn);
String strSqlQuery = "select * from tbl_CompanyJobCode";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
string SelectedCompanyJobCode;
DropDownList dropdownlist = (DropDownList)sender;
try
{
DataSet ds = new DataSet();
da.Fill(ds);
dropdownlist.DataSource = ds.Tables[0];
dropdownlist.DataTextField = "CompanyJobDesc";
dropdownlist.DataValueField = "CompanyJobCode";
// dropdownlist.DataBind();
DataGridItem container = (DataGridItem)dropdownlist.NamingContainer;
SelectedCompanyJobCode =
Convert.ToString(((DataRowView)container.DataItem)["CompanyJobCode"]);
if (dropdownlist.Items.FindByValue(SelectedCompanyJobCode) != null)
dropdownlist.Items.FindByValue(SelectedCompanyJobCode.ToString()).Selected =
true;
}
catch (Exception ex)
{
Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
============================================================================
=======
So, I'm stuck.
- Kilic