471,887 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

DataGrid Problem

I am trying display the contents of a table in a web page, select
certain rows from that table and then display the fields that I have
selected (now table columns) as text in a Label object. Amazingly I have
managed to display the table - no problem, I can select rows - no
problem, BUT I can't work out how to display more than one of the fields
(now columns in the table in the web page). I can display one field OK,
but not more than one field and its driving me bonkers. All of my
confusion is related to how you reference a DataKeyField in a DataGrid.

The two fields that I am selecting are ID and a Customer taken from the

SELECT Field_ID as ID, Field_Customer as Customer FROM Table_Customer

I've worked out that the DataGrid DataKeys property (if it is a
property) controls which field is passed to the the instance of the
DataGrid so

<asp:datagrid id="DemoGrid" runat="server" DataKeyField="customer"

and then

gridSelections.AppendFormat("{0} ",


will result in the customer field being in the result

and if I change the DataKeyField to id then

<asp:datagrid id="DemoGrid" runat="server" DataKeyField="id"

will result the id field being in the result of

gridSelections.AppendFormat("{0} ",


Thing is I want both customer and id to be in the result.

There that is my problem (well the real problem is lack of knowledge of
DataGrids and C# in general - but thats my problem).

I hope the question makes sense and if I have offended anyone with the
question I apologize but suggest you try to take it easy.

Here's the complete c# code behind the scene and also the HTML code of
the web form.

Other than begging and pleading HELP , I would be very grateful if
someone could shine light upon the darkness of my ignorance.


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

namespace WebApplication1


/// <summary>
/// Summary description for WebForm1.
/// </summary>

public class WebForm1 : System.Web.UI.Page
protected System.Web.UI.WebControls.DataGrid DemoGrid;
protected System.Web.UI.WebControls.Label ResultsInfo;
protected System.Web.UI.WebControls.Button GetSelections;

private void Page_Load(object sender, System.EventArgs e)
//Create a SqlConnection object.
//Modify the connection string as necessary for your environment.
SqlConnection cn = new
SqlConnection("Server=MyServer;database=MyDatabase ;UID=sa;PWD=MyPassword
SqlCommand cmd = new SqlCommand("SELECT Field_ID as ID, Field_Customer
as Customer FROM Table_Customer",cn);
SqlDataReader reader = cmd.ExecuteReader();
DemoGrid.DataSource = reader;


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.DemoGrid.SelectedIndexChanged += new
System.EventHandler(this.DemoGrid_SelectedIndexCha nged);
this.GetSelections.Click += new
this.Load += new System.EventHandler(this.Page_Load);

private void GetSelections_Click(object sender, System.EventArgs e)
int rowCount = 0;
StringBuilder gridSelections = new StringBuilder();
//Loop through each DataGridItem, and determine which CheckBox controls
//have been selected.
foreach(DataGridItem DemoGridItem in DemoGrid.Items)
CheckBox myCheckbox = (CheckBox)DemoGridItem.Cells[0].Controls[1];
if(myCheckbox.Checked == true)
gridSelections.AppendFormat("{0} ",

ListBox myListBox = (ListBox)DemoGridItem.Cells[1].Controls[1];
gridSelections.AppendFormat( " " +
myListBox.SelectedItem.Text.ToString()+ " <br>");
} gridSelections.Append("<hr>");

gridSelections.AppendFormat("Total number selected is: {0}",

ResultsInfo.Text = gridSelections.ToString();
private void DemoGrid_SelectedIndexChanged(object sender,
System.EventArgs e)

public SqlDataReader GetReportNames ()

//Polulates the list box for report names
SqlConnection conn = new SqlConnection("server=MyServer; uid=sa;
pwd=MyPassword; Database=MyDatabase");
String sqlConnection = "select Report, Name from TableReports";
SqlCommand sqlCommand = new SqlCommand(sqlConnection, conn);
SqlDataReader sqlDataReader;
sqlDataReader = sqlCommand.ExecuteReader();
return sqlDataReader;
HTML WebForm

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="_JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:label id="ResultsInfo" style="Z-INDEX: 103; LEFT: 472px;
POSITION: absolute; TOP: 112px"
runat="server" Width="304px" Height="112px"></asp:label>
<asp:datagrid id="DemoGrid" runat="server" DataKeyField="customer"
<asp:TemplateColumn HeaderText="Run">
<asp:CheckBox ID="myCheckbox" Runat="server" />
<asp:TemplateColumn HeaderText="Report">
<asp:ListBox ID="myListbox" Rows="1" Width="150px"
SelectionMode="Single" Runat="server"
DataSource = "<%# GetReportNames() %>"
DataTextField="ReportName" DataValueField="Report" />
</asp:datagrid><asp:button id="GetSelections" style="Z-INDEX: 102;
LEFT: 472px; POSITION: absolute; TOP: 24px"
runat="server" Text="Build Request"

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
1 2383

Here is a way that you can read the contents of cells from a datagrid. This assumes that you are displaying the data in TextBoxes. You would have to adjust the code if you are using a different type of control or not control at all. I hope this helps. Cheers, Danny!
string categoryName;
string categoryDescription;
TextBox tb;
tb = (TextBox) e.Item.Cells[2].Controls[0];
categoryName = tb.Text;
tb = (TextBox) e.Item.Cells[3].Controls[0];
categoryDescription = tb.Text

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris Plowman | last post: by
reply views Thread by Emerson | last post: by
5 posts views Thread by Jeff | last post: by
4 posts views Thread by The Alchemist | last post: by
9 posts views Thread by tshad | last post: by
4 posts views Thread by tshad | last post: by
9 posts views Thread by rn5a | last post: by
2 posts views Thread by =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

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.