Hi moondaddy,
Thank you for using Microsoft Newsgroup Service. Based on your description,
you want to get some information on how to add some columns(with text or
Images) in the ASP.NET's DataGrid control, and the controls' value is set
as the data retrieved from a SQLServer Database. Is my understanding of
your problem correct?
If so, I think George Durzi's suggestion is quite good, the TemplateColumn
of the DataGrid control provided the powerful functions for you to
customize a certain DataBind column in DataGrid. You may add different
kinds of serverside or normal html controls into a Template column and set
databind attribute for it. For more detailed description on how to use the
ASP.NET DataGrid's template column, you can view the following article on
MSDN:
http://msdn.microsoft.com/library/en-us/dnaspp/html/creatingcustomcolumns.as
p?frame=true
Also, I've made a generic sample, I used a simulated DataTable to perform
the DataSource, you may change it to your actual DataSource( query from
SQLServer) according to your own situation. Here is the source code:
-------------------------aspx file------------------------------
<%@ Page language="c#" Codebehind="DataGridBind.aspx.cs"
AutoEventWireup="false" Inherits="NewWebApp.DataGridBind"
smartNavigation="False"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridBind</title>
<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"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="600" align="center" border="1">
<tr>
<td><asp:datagrid id="dgBind" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index" ReadOnly="True"
HeaderText="Index" />
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Runat='server' Text='<%#
DataBinder.Eval( Container.DataItem,"name") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"grade") %>
</ItemTemplate>
<EditItemTemplate>
<asp
ropDownList runat="server" ID="lstGrade">
<asp:ListItem Value="low">Low</asp:ListItem>
<asp:ListItem Value="normal"
Selected="True">Normal</asp:ListItem>
<asp:ListItem Value="high">High</asp:ListItem>
</asp
ropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:Image id="imgImage" Runat="server" ImageUrl='<%#
DataBinder.Eval(Container.DataItem,"imageurl") %>' >
</asp:Image>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUrl" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"imageurl") %>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"description") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" TextMode=MultiLine Runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"description") %>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" HeaderText="Operation" />
</Columns>
</asp:datagrid></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
--------------------------------aspx.cs codebehind
file------------------------------------------------
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;
namespace NewWebApp
{
/// <summary>
/// Summary description for DataGridBind.
/// </summary>
public class DataGridBind : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgBind;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
string baseurl = "
http://www.cacti1104.com/images/stuff/icon/001_00";
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("grade");
tb.Columns.Add("imageurl");
tb.Columns.Add("description");
for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();
newrow["index"] = index.ToString();
newrow["name"] = "name" + index.ToString();
newrow["imageurl"] = baseurl + index.ToString() + ".gif";
newrow["description"] = "default description:" + index.ToString();
switch(index%3)
{
case 0:
newrow["grade"] = "low";
break;
case 1:
newrow["grade"] = "normal";
break;
case 2:
newrow["grade"] = "high";
break;
}
tb.Rows.Add(newrow);
}
dgBind.DataSource = tb;
dgBind.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgBind.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgBind_CancelComm
and);
this.dgBind.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgBind_EditComman
d);
this.dgBind.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgBind_UpdateComm
and);
this.dgBind.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgBind_ItemDataBound
);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgBind_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
string grade = drv["grade"].ToString();
DropDownList lstGrade = (DropDownList)e.Item.Cells[2].Controls[1];
for(int i=0;i<lstGrade.Items.Count;i++)
{
if(lstGrade.Items
.Value.Equals(grade))
{
lstGrade.SelectedIndex = i;
break;
}
}
}
}
private void dgBind_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//set edit index
dgBind.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
private void dgBind_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgBind.EditItemIndex = -1;
BindGrid();
}
private void dgBind_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//add update code here
dgBind.EditItemIndex = -1;
BindGrid();
}
}
}
Hope this will be helpful to you. If you have any questions on it, please
feel free to let me know.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)