<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Test data...
DataTable dtt = new DataTable();
dtt.Columns.Add("MyText", typeof(string));
dtt.Columns.Add("MyText2", typeof(string));
dtt.Rows.Add("My record 1", "123");
dtt.Rows.Add("My record 2", "456");
dtt.Rows.Add("My record 3", "789");
GridView1.DataSource = dtt;
GridView1.DataBind();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="My Column">
<ItemTemplate>
<asp:Label Text='<%# Eval("MyText").ToString() + " - " + Eval("MyText2").ToString() %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
ASKER
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
DataTable dtt = new DataTable();
dtt.Columns.Add("NameDepartment", typeof(string));
dtt.Columns.Add("DateCreated", typeof(string));
dtt.Columns.Add("Form", typeof(string));
dtt.Columns.Add("FormId", typeof(int));
dtt.Columns.Add("Status", typeof(string));
dtt.Rows.Add("Dan Smith Engineering", new DateTime(2011, 5, 23), "Pre", 1, "Closed");
dtt.Rows.Add("Dan Smith Engineering", new DateTime(2011, 5, 23), "Post", 2, "Closed");
dtt.Rows.Add("Mark Jones Accounting", new DateTime(2011, 6, 1), "Pre", 10, "Closed");
dtt.Rows.Add("Mark Jones Accounting", new DateTime(2011, 6, 1), "Post", 15, "Open");
dtt.Rows.Add("Mark Jones Accounting", new DateTime(2011, 6, 1), "Test", 18, "Pending");
// I'm using a DataReader to make it more DB friendly.
using (IDataReader dr = dtt.CreateDataReader())
{
TableCell lastCellNameDeparment = null;
TableCell lastCellCreatedTableCell = null;
int innerRecordCount = 0;
while (dr.Read())
{
TableRow tbRow = new TableRow();
if (lastCellNameDeparment == null || lastCellNameDeparment.Text != dr["NameDepartment"].ToString())
{
if (lastCellNameDeparment != null)
{
lastCellNameDeparment.RowSpan = innerRecordCount;
lastCellCreatedTableCell.RowSpan = innerRecordCount;
innerRecordCount = 0;
}
lastCellNameDeparment = new TableCell();
lastCellNameDeparment.Text = dr["NameDepartment"].ToString();
tbRow.Cells.Add(lastCellNameDeparment);
lastCellCreatedTableCell = new TableCell();
lastCellCreatedTableCell.Text = Convert.ToDateTime(dr["DateCreated"]).ToString("d");
tbRow.Cells.Add(lastCellCreatedTableCell);
}
TableCell cellForm = new TableCell();
cellForm.Text = dr["Form"].ToString();
tbRow.Cells.Add(cellForm);
TableCell cellViewFileCopy = new TableCell();
HyperLink myLink = new HyperLink();
myLink.Text = dr["FormId"].ToString();
myLink.NavigateUrl = "View.aspx?Id=" + dr["FormId"].ToString();
cellViewFileCopy.Controls.Add(myLink);
tbRow.Cells.Add(cellViewFileCopy);
TableCell cellStatus = new TableCell();
cellStatus.Text = dr["Status"].ToString();
tbRow.Cells.Add(cellStatus);
TableCell cellEdit = new TableCell();
HyperLink myLink2 = new HyperLink();
myLink2.Text = "Edit";
myLink2.NavigateUrl = "Edit.aspx?Id=" + dr["FormId"].ToString();
cellEdit.Controls.Add(myLink2);
tbRow.Cells.Add(cellEdit);
Table1.Rows.Add(tbRow);
innerRecordCount++;
}
lastCellNameDeparment.RowSpan = innerRecordCount;
lastCellCreatedTableCell.RowSpan = innerRecordCount;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Table ID="Table1" runat="server" GridLines="Both">
<asp:TableHeaderRow>
<asp:TableHeaderCell>
Name & Department
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Date Created
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Form
</asp:TableHeaderCell>
<asp:TableHeaderCell>
View File Copy
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Status
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Edit
</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</form>
</body>
</html>
ASKER
// TableCell.HorizontalAlign
TableCell cellStatus = new TableCell();
cellStatus.HorizontalAlign = HorizontalAlign.Center;
// HyperLink.ImageUrl
HyperLink myLink = new HyperLink();
myLink.ImageUrl = "Images/MyImage.gif";
Also, all the web controls has the CssClass property, that you can use to set multiple CSS (Cascading Style Sheets) classes.
ASKER
ASKER
using (SqlConnection cn = new SqlConnection("MyConnectionString"))
{
SqlCommand cm = new SqlCommand("MyStoredProcedure", cn);
cm.CommandType = CommandType.StoredProcedure;
cn.Open();
using (IDataReader dr = cm.ExecuteReader())
{
TableCell lastCellNameDeparment = null;
TableCell lastCellCreatedTableCell = null;
int innerRecordCount = 0;
while (dr.Read())
{
TableRow tbRow = new TableRow();
if (lastCellNameDeparment == null || lastCellNameDeparment.Text != dr["NameDepartment"].ToString())
{
if (lastCellNameDeparment != null)
{
lastCellNameDeparment.RowSpan = innerRecordCount;
lastCellCreatedTableCell.RowSpan = innerRecordCount;
innerRecordCount = 0;
}
lastCellNameDeparment = new TableCell();
lastCellNameDeparment.Text = dr["NameDepartment"].ToString();
tbRow.Cells.Add(lastCellNameDeparment);
lastCellCreatedTableCell = new TableCell();
lastCellCreatedTableCell.Text = Convert.ToDateTime(dr["DateCreated"]).ToString("d");
tbRow.Cells.Add(lastCellCreatedTableCell);
}
TableCell cellForm = new TableCell();
cellForm.Text = dr["Form"].ToString();
tbRow.Cells.Add(cellForm);
TableCell cellViewFileCopy = new TableCell();
HyperLink myLink = new HyperLink();
myLink.Text = dr["FormId"].ToString();
myLink.NavigateUrl = "View.aspx?Id=" + dr["FormId"].ToString();
cellViewFileCopy.Controls.Add(myLink);
tbRow.Cells.Add(cellViewFileCopy);
TableCell cellStatus = new TableCell();
cellStatus.Text = dr["Status"].ToString();
tbRow.Cells.Add(cellStatus);
TableCell cellEdit = new TableCell();
HyperLink myLink2 = new HyperLink();
myLink2.Text = "Edit";
myLink2.NavigateUrl = "Edit.aspx?Id=" + dr["FormId"].ToString();
cellEdit.Controls.Add(myLink2);
tbRow.Cells.Add(cellEdit);
Table1.Rows.Add(tbRow);
innerRecordCount++;
}
lastCellNameDeparment.RowSpan = innerRecordCount;
lastCellCreatedTableCell.RowSpan = innerRecordCount;
}
}
ASKER
dr["YourCorrectColumnName"]
ASKER
ASKER
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// How many groups you want to show per page?
const int GroupsPerPage = 10;
protected void Page_Load(object sender, EventArgs e)
{
DataTable dtt = new DataTable();
dtt.Columns.Add("NameDepartment", typeof(string));
dtt.Columns.Add("DateCreated", typeof(string));
dtt.Columns.Add("Form", typeof(string));
dtt.Columns.Add("FormId", typeof(int));
dtt.Columns.Add("Status", typeof(string));
// Duplicate sample data...
for (int i = 0; i < 100; i++)
{
dtt.Rows.Add("Dan Smith Engineering", new DateTime(2011, 5, 23), "Pre" + i, 1, "Closed");
dtt.Rows.Add("Dan Smith Engineering", new DateTime(2011, 5, 23), "Post" + i, 2, "Closed");
dtt.Rows.Add("Mark Jones Accounting", new DateTime(2011, 6, 1), "Pre" + i, 10, "Closed");
dtt.Rows.Add("Mark Jones Accounting", new DateTime(2011, 6, 1), "Post" + i, 15, "Open");
dtt.Rows.Add("Mark Jones Accounting", new DateTime(2011, 6, 1), "Test" + i, 18, "Pending");
}
int currentPage = this.CurrentPage;
// I'm using a DataReader to make it more DB friendly.
using (IDataReader dr = dtt.CreateDataReader())
{
TableCell lastCellNameDeparment = null;
TableCell lastCellCreatedTableCell = null;
int innerRecordCount = 0;
int groupCount = 0;
int groupCountStart = (currentPage - 1) * GroupsPerPage;
while (dr.Read())
{
TableRow tbRow = new TableRow();
if (lastCellNameDeparment == null || lastCellNameDeparment.Text != dr["NameDepartment"].ToString())
{
groupCount++;
if (lastCellNameDeparment != null)
{
lastCellNameDeparment.RowSpan = innerRecordCount;
lastCellCreatedTableCell.RowSpan = innerRecordCount;
innerRecordCount = 0;
}
if (groupCount >= GroupsPerPage * currentPage)
break;
lastCellNameDeparment = new TableCell();
lastCellNameDeparment.Text = dr["NameDepartment"].ToString();
tbRow.Cells.Add(lastCellNameDeparment);
lastCellCreatedTableCell = new TableCell();
lastCellCreatedTableCell.Text = Convert.ToDateTime(dr["DateCreated"]).ToString("d");
tbRow.Cells.Add(lastCellCreatedTableCell);
}
if (groupCount < groupCountStart)
continue;
TableCell cellForm = new TableCell();
cellForm.Text = dr["Form"].ToString();
tbRow.Cells.Add(cellForm);
TableCell cellViewFileCopy = new TableCell();
HyperLink myLink = new HyperLink();
myLink.Text = dr["FormId"].ToString();
myLink.NavigateUrl = "View.aspx?Id=" + dr["FormId"].ToString();
cellViewFileCopy.Controls.Add(myLink);
tbRow.Cells.Add(cellViewFileCopy);
TableCell cellStatus = new TableCell();
cellStatus.Text = dr["Status"].ToString();
tbRow.Cells.Add(cellStatus);
TableCell cellEdit = new TableCell();
HyperLink myLink2 = new HyperLink();
myLink2.Text = "Edit";
myLink2.NavigateUrl = "Edit.aspx?Id=" + dr["FormId"].ToString();
cellEdit.Controls.Add(myLink2);
tbRow.Cells.Add(cellEdit);
Table1.Rows.Add(tbRow);
innerRecordCount++;
}
if (innerRecordCount > 0)
{
lastCellNameDeparment.RowSpan = innerRecordCount;
lastCellCreatedTableCell.RowSpan = innerRecordCount;
}
}
// Create paging controls...
TableRow tbFootRow = new TableRow();
TableCell cellFoot = new TableCell();
cellFoot.ColumnSpan = 6;
cellFoot.HorizontalAlign = HorizontalAlign.Center;
HyperLink linkBack = new HyperLink();
linkBack.Text = "< Previous";
linkBack.NavigateUrl = "BuildTableExample.aspx?page=" + (currentPage - 1);
linkBack.Enabled = currentPage - 1 > 0;
cellFoot.Controls.Add(linkBack);
Label lblSep = new Label();
lblSep.Text = " | ";
cellFoot.Controls.Add(lblSep);
HyperLink linkNext = new HyperLink();
linkNext.Text = "Next >";
linkNext.NavigateUrl = "BuildTableExample.aspx?page=" + (currentPage + 1);
cellFoot.Controls.Add(linkNext);
tbFootRow.Controls.Add(cellFoot);
Table1.Rows.Add(tbFootRow);
}
public int CurrentPage
{
get
{
int value = 0;
int.TryParse(Request.QueryString["page"], out value);
if (value <= 0)
value = 1;
return value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Table ID="Table1" runat="server" GridLines="Both">
<asp:TableHeaderRow>
<asp:TableHeaderCell>
Name & Department
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Date Created
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Form
</asp:TableHeaderCell>
<asp:TableHeaderCell>
View File Copy
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Status
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Edit
</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</form>
</body>
</html>
ASKER
ASKER
The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications
TRUSTED BY
<asp:TemplateField HeaderText="Random" HeaderStyle-HorizontalAlig
<ItemTemplate>
<asp:Label ID="lblthingsName" runat="server" Text = '<%# Eval("randomData") & ", " & Eval("sort") & ", " & Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
I've combine data from three different columns in the database table into one column in the Gridview and added some commas between.
You could also retrieve your data from the database and put it into a new datatable. Then you can re-order it any way you like and bind the new datatable to the gridview.