?
Solved

Get Value in DataGrid

Posted on 2008-10-14
6
Medium Priority
?
298 Views
Last Modified: 2013-12-17
I am trying to change the Cell Color of my Data Grid depending on the value of a Cell and for some reason the value of my cell seems to be returning a null value.

The function DataGrid1_ItemCreated is called and this line should return the value of the cell, but is returning "".
string test = e.Item.Cells[0].Text;

The Data Grid does load with no issues after proceeding through this function.

Any help would be greatly appreciated, I have included the full code for your review.


public class BranchLicensing : System.Web.UI.Page
	{
 
		string PageID = "BranchLicensing.aspx.cs";
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
 
		GatewaySales.Modules.Common.Library objLibrary = new GatewaySales.Modules.Common.Library();
		GatewaySales.Modules.Common.BaseSecurity  objBaseSecurity = new GatewaySales.Modules.Common.BaseSecurity();
 
 
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			PageInit();
 
			if(!IsPostBack)
			{
				LoadBranchLicenses();
			}
		}
 
		public System.Data.DataTable LoadBranchLicenses() 
		{
			// connect to database
			SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings["Integra_ConnectionString"]);
			conn.Open();
			string selectstmt = "";
			selectstmt = "select State, CONVERT (varchar,ExpirationDate, 101) as ExpirationDate, Lender1stMtg, Lender2ndMtg, Broker1stMtg, Broker2ndMtg from custom_licensingbranch where branchid = 51 order by state";
			
			SqlCommand myCommand = new SqlCommand(selectstmt,conn);
			SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
			System.Data.DataTable BranchLicenses = new System.Data.DataTable(); 
			adapter.Fill(BranchLicenses);
 
			DataGrid1.DataSource = BranchLicenses;
			DataGrid1.DataBind();
				
			conn.Close();
			
			return BranchLicenses;
		}
 
		protected void DataGrid1_ItemCreated( System.Object sender, System.Web.UI.WebControls.DataGridItemEventArgs e )  
		{
			
			if ( e.Item.ItemType == ListItemType.Item ||
				e.Item.ItemType == ListItemType.AlternatingItem ||
				e.Item.ItemType == ListItemType.SelectedItem )  
			{
 
				string test = e.Item.Cells[0].Text;
 
				if(test == "PA")
				{
					e.Item.Cells[0].ForeColor = System.Drawing.Color.Green;
				
				}
			}
 
		}
 
 
 
 
 
 
 
<form id="Form1" method="post" runat="server" class="appbg">
			<table width="300" cellpadding="0" cellspacing="0" bgcolor="#ffffff" class="appbg">
				<TBODY>
					<tr>
						<td colspan="3" width="100%">
							<!--#include file="../../includes/topNav.aspx"-->
						</td>
					</tr>
					<tr>
						<td width="1%" valign="top">
							<!--#include file="../../includes/leftNav.aspx"-->
						</td>
						<td width="1%" valign="top">
							&nbsp;&nbsp;
						</td>
						<td width="1%">
							<!-- ******* Contents will go here *********** !-->
							<div style="BORDER-RIGHT:black 0px solid; BORDER-TOP:black 0px solid; BORDER-LEFT:black 0px solid; WIDTH:633px; BORDER-BOTTOM:black 0px solid; HEIGHT:445px">
								<table border="0" width="100%">
									<TBODY>
										<tr>
											<td class="contentbg"><b><IMG src="<%=getImagePath()%>/Images/PageHead.bmp">&nbsp;State Desk Licensing Information</b><HR>
											</td>
										</tr>
										
										<tr>
											<td>
												<asp:datagrid id="DataGrid1" AllowPaging="False" runat="server" ShowFooter="True"
													AllowSorting="True" GridLines="Horizontal" CellPadding="2" BorderWidth="1px" Width="100%"
														AutoGenerateColumns="False" OnItemDataBound="DataGrid1_ItemCreated">
												<SelectedItemStyle></SelectedItemStyle>
												<AlternatingItemStyle BackColor="Silver"></AlternatingItemStyle>
												<HeaderStyle CssClass="sectionheader"></HeaderStyle>
												<Columns>
													<asp:TemplateColumn HeaderText="State" HeaderStyle-Font-Bold="True">
													<ItemTemplate>
														<asp:Label Runat="server" ID="dglstate"><%# DataBinder.Eval(Container, "DataItem.State") %>
													</asp:Label>
													</ItemTemplate>
													</asp:TemplateColumn>
													<asp:TemplateColumn HeaderText="Expiration Date" HeaderStyle-Font-Bold="True">
														<ItemTemplate>
															<asp:Label Runat="server" ID="dglexpdate"><%# DataBinder.Eval(Container, "DataItem.ExpirationDate") %>
															</asp:Label>
														</ItemTemplate>
													</asp:TemplateColumn>
													<asp:TemplateColumn HeaderText="1st Lien Mtg License?" HeaderStyle-Font-Bold="True">
														<ItemTemplate>
															<asp:Label Runat="server" ID="Label3"><%# DataBinder.Eval(Container, "DataItem.Lender1stMtg") %>
															</asp:Label>
														</ItemTemplate>
													</asp:TemplateColumn>
													<asp:TemplateColumn HeaderText="2nd Lien Mtg License?" HeaderStyle-Font-Bold="True">
														<ItemTemplate>
															<asp:Label TabIndex="0" Runat="server" ID="Label4"><%# DataBinder.Eval(Container, "DataItem.Lender2ndMtg") %>
															</asp:Label>
														</ItemTemplate>
													</asp:TemplateColumn>
													<asp:TemplateColumn HeaderText="1st Lien Broker License?" HeaderStyle-Font-Bold="True">
														<ItemTemplate>
															<asp:Label TabIndex="0" Runat="server" ID="Label5"><%# DataBinder.Eval(Container, "DataItem.Broker1stMtg") %>
															</asp:Label>
														</ItemTemplate>
													</asp:TemplateColumn>
													<asp:TemplateColumn HeaderText="2nd Lien Broker License?" HeaderStyle-Font-Bold="True">
														<ItemTemplate>
															<asp:Label TabIndex="0" Runat="server" ID="Label6"><%# DataBinder.Eval(Container, "DataItem.Broker2ndMtg") %>
															</asp:Label>
														</ItemTemplate>
													</asp:TemplateColumn>
												</Columns>									
											</asp:DataGrid>
											</td>
										</tr>
										<tr>
											<td>
												
												<P></form>

Open in new window

0
Comment
Question by:dtadin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22712566
Do it in ItemDataBound not ItemCreated.
0
 

Author Comment

by:dtadin
ID: 22712752
I am using OnItemDataBound="DataGrid1_ItemCreated"



thank you
<asp:datagrid id="DataGrid1" AllowPaging="False" runat="server" ShowFooter="True"
													AllowSorting="True" GridLines="Horizontal" CellPadding="2" BorderWidth="1px" Width="100%"
														AutoGenerateColumns="False" OnItemDataBound="DataGrid1_ItemCreated">

Open in new window

0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22713232
you are using template .. that is why value is not bind to cell

you need to use FindControl() and then cast the object....for example..

((Label)e.Item.FindControl("lblName")).Text
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:dtadin
ID: 22713750
I tried updating my function to

  string test = ((Label)e.Item.FindControl("dglstate")).Text;

Still no luck, test is still returning a value of "".

0
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 750 total points
ID: 22714196
can you check if your control support e.row
string test = ((Label)e.Row.FindControl("dglstate")).Text;
0
 

Author Comment

by:dtadin
ID: 22714229
Nope, I get "System.Web.UI.WebControls.DataGridItemEventArgs' does not contain a definition for 'Row'"



0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question