473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 1996

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

Similar topics

1
by: Dave | last post by:
Hello. Can anybody explain me how to use TemplateColumn?
2
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.