473,666 Members | 2,044 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.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>
Jul 21 '05 #1
0 2024

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

Similar topics

1
1518
by: Dave | last post by:
Hello. Can anybody explain me how to use TemplateColumn?
2
2118
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 my Datagrid. Something like: Dim tc as New TemplateColumn
2
5853
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 use template columns as they seem to be a little difficult to create at runtime. Any assistance would be very greatly appreciated. private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if...
0
1440
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 User control in the Datagrid... and that is a run time user control in the datagrid.. without using the <templatecolumn> in the datagrid... and on ItemCreated Event i 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 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...
0
1173
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 horizontalalign="Left" wrap="False"></ItemStyle> <ItemTemplate> <%#Databinder.Eval(Container.DataItem, "isVoid")%> </ItemTemplate>
2
2449
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 align="right"><b>Name:</b></td> <td><%# DataBinder.Eval(Container.DataItem, "name") %></td>
3
7376
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" HeaderText="ID" SortExpression="PID"> <ItemTemplate> <asp:Label ID="lblPID" Text=<%# Container.DataItem("PID") %> runat="server"/>
2
3279
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 + e.Item.Cells.Text; /* column index 2 and 3 are boundcolumn */ The issue that I have is that the column header (index 1) would be blank after it.
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.