Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > add TemplateColumn(dropdown) to DataGrid at runtime

Reply
Thread Tools

add TemplateColumn(dropdown) to DataGrid at runtime

 
 
Kilic
Guest
Posts: n/a
 
      04-27-2004
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[i] != "CompanyJobCode")
{
bCol = new BoundColumn();
bCol.HeaderText = v_ColDesc[i];
bCol.DataField = v_ColCode[i];
bCol.SortExpression = v_ColCode[i];
if (v_ColCode[i] == "EmployeeID")
bCol.ReadOnly = true;

_DataGrid.Columns.Add(bCol);
}
else
{
tCol = new TemplateColumn();
tCol.HeaderText = v_ColDesc[i];
tCol.SortExpression = v_ColCode[i];
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(SelectedCompanyJob Code) != null)

dropdownlist.Items.FindByValue(SelectedCompanyJobC ode.ToString()).Selected =
true;

}
catch (Exception ex)
{
Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
}
finally
{
conn.Close();
conn.Dispose();
}
}
}

================================================== ==========================
=======

So, I'm stuck.

- Kilic


 
Reply With Quote
 
 
 
 
Amar
Guest
Posts: n/a
 
      04-28-2004
Your code seems correct i don't see any problem i have done the same
thing and i never had any problem...maybe somewhere else is the
problem....

"Kilic" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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[i] != "CompanyJobCode")
> {
> bCol = new BoundColumn();
> bCol.HeaderText = v_ColDesc[i];
> bCol.DataField = v_ColCode[i];
> bCol.SortExpression = v_ColCode[i];
> if (v_ColCode[i] == "EmployeeID")
> bCol.ReadOnly = true;
>
> _DataGrid.Columns.Add(bCol);
> }
> else
> {
> tCol = new TemplateColumn();
> tCol.HeaderText = v_ColDesc[i];
> tCol.SortExpression = v_ColCode[i];
> 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(SelectedCompanyJob Code) != null)
>
> dropdownlist.Items.FindByValue(SelectedCompanyJobC ode.ToString()).Selected =
> true;
>
> }
> catch (Exception ex)
> {
> Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
> }
> finally
> {
> conn.Close();
> conn.Dispose();
> }
> }
> }
>
> ================================================== ==========================
> =======
>
> So, I'm stuck.
>
> - Kilic

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add controls at runtime JezB ASP .Net 4 07-26-2006 05:02 AM
Add DropdownList to DataGrid at Runtime James ASP .Net 2 07-14-2006 06:18 PM
Runtime.exec(String[]) Doesn't Always Work, bBut Runtime.exec(String) Does Hal Vaughan Java 11 05-22-2006 04:49 PM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth ASP .Net Datagrid Control 0 04-05-2005 03:58 AM
Re: How to add eventHandlers at runtime? Scott Meddows ASP .Net 1 09-06-2003 08:07 PM



Advertisments