473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add TemplateColumn (dropdown) to DataGrid at runtime

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

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>pdlLKLZ8oU+T62eYJhM2WQ==</Id>
Jul 21 '05 #1
0 2006

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1510
by: Dave | last post by:
Hello. Can anybody explain me how to use TemplateColumn?
2
2113
by: Ricardo | last post by:
Hi, How can I insert a TemplateColumn on a Datagrid on the fly? Basically I'm after how to create a TemplateColumn, add a button and a label to its control list and add the TemplateColumn to...
2
5843
by: Steve Pierce | last post by:
I am having some issues with a runtime dropdownlist in a datagrid. The issue is that I cannot get ViewState to fill the selected index of a runtime dropdown properly on postback. I do not want to...
0
1436
by: Gujju | last post by:
Hi all, Have a question from the following post to create user control in a Datagrid.... http://www.dotnet247.com/247reference/msgs/45/225337.aspx I have a question.... I can create the...
0
420
by: Kilic Beg via .NET 247 | last post by:
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...
0
1169
by: Vagabond Software | last post by:
I am fairly new to ASP.NET, so bear with me... I have a Datagrid with a data-bound DropDownList in the TemplateColumn. Here is the HTML code: <asp:TemplateColumn HeaderText="Void"> <ItemStyle...
2
2444
by: MrCrool | last post by:
Hi I need to be able to handle the following ASP programming in pure C# code: <asp:TemplateColumn HeaderText="Customer Information"> <ItemTemplate> <table border="0"> <tr> <td...
3
7368
by: rn5a | last post by:
A DataGrid has the following TemplateColumn: <asp:DataGrid ID="dgCart"...OnSortCommand="SortGrid" AllowSorting="true"....> <Column> <asp:TemplateColumn HeaderImageUrl="Images\Up.gif"...
2
3272
by: =?Utf-8?B?RXVnZW5l?= | last post by:
Hi, I use a datagrid with asp:templatecolumn, and handles the datagrid's ItemDataBound event, whereby I assign a value to this asp:templatecolumn text. e.Item.Cells.Text = e.Item.Cells.Text +...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.