void BindGrid() { // create the connection, DataAdapter and DataSet string connString = "server=(local);database=Northwind;uid=sa;pwd=;"; string sql = "SELECT * FROM Employees"; SqlConnection conn = new SqlConnection(connString); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet();
// fill the dataset and retrieve the default view da.Fill(ds, "Employees"); DataView dv = ds.Tables["Employees"].DefaultView;
// sort the view according to the value stored in the ViewState, in any if (ViewState["SortExpression"] != null) dv.Sort = ViewState["SortExpression"].ToString(); Datagrid1.DataSource = dv; Datagrid1.DataBind(); }
// Create an instance of the 'CurrentCellChanged' EventHandler. private void CallCurrentCellChanged() { myDataGrid.CurrentCellChanged += new EventHandler(Grid_CurCellChange); }// Raise the event when focus on DataGrid cell changes. protected void Grid_CurCellChange(object sender, EventArgs e) { // String variable used to show message. string myString = "CurrentCellChanged event raised, cell focus is at "; // Get the co-ordinates of the focussed cell. string myPoint = myDataGrid.CurrentCell.ColumnNumber + "," + myDataGrid.CurrentCell.RowNumber; // Create the alert message. myString = myString + "(" + myPoint + ")"; // Show Co-ordinates when CurrentCellChanged event is raised. MessageBox.Show(myString, "Current cell co-ordinates"); }
private void myDataGrid_CurrentCellChanged(object sender, System.EventArgs e) { MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber + ", Row is " + myDataGrid.CurrentCell.RowNumber + ", Value is " + myDataGrid[myDataGrid.CurrentCell]//获得CurrentCell的值 ); }
在DG的ItemCommand里加这句话
TextBox1.Text = e.item.cells[列值].text;
<HTML>
<body>
<form method="post" runat="server">
<asp:label id="MoreInfo" runat="server"></asp:label><br>
<asp:textbox id="textbox1" runat="server" Width="200px"></asp:textbox><br>
<asp:datagrid id="Datagrid1" runat="server" SelectedItemStyle-Font-Bold="true" SelectedItemStyle-BackColor="Red"
SelectedItemStyle-ForeColor="Yellow" AlternatingItemStyle-Font-Italic="true" AlternatingItemStyle-ForeColor="Maroon"
AlternatingItemStyle-BackColor="LightYellow" ItemStyle-ForeColor="DarkBlue" ItemStyle-BackColor="LightCyan"
FooterStyle-BorderWidth="5" FooterStyle-BorderColor="Red" HeaderStyle-BorderWidth="5" HeaderStyle-BorderColor="Red"
HeaderStyle-BackColor="Yellow" HeaderStyle-ForeColor="Red" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="10"
OnSelectedIndexChanged="Datagrid1_SelectionChanged" OnSortCommand="Datagrid1_Sort" AllowSorting="true"
Width="100%" AutoGenerateColumns="False" DataKeyField="EmployeeID">
<Columns>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID" SortExpression="EmployeeID" />
<asp:BoundColumn HeaderText="Title" ItemStyle-Width="50px" DataField="TitleOfCourtesy" SortExpression="TitleOfCourtesy" />
<asp:BoundColumn HeaderText="Last Name" ItemStyle-Width="150px" DataField="LastName" SortExpression="LastName" />
<asp:BoundColumn HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
<asp:ButtonColumn CommandName="Select" ItemStyle-Width="20px" Text="<img border=0 src=message.gif>" />
</Columns>
</asp:datagrid></form>
</body>
</HTML>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;namespace WebApplication2
{
/// <summary>
/// DataGrid4 的摘要说明。
/// </summary>
public class DataGrid4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label MoreInfo;
protected System.Web.UI.WebControls.TextBox textbox1;
protected System.Web.UI.WebControls.DataGrid Datagrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
BindGrid();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Datagrid1.SelectedIndexChanged += new System.EventHandler(this.Datagrid1_SelectionChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
void BindGrid()
{
// create the connection, DataAdapter and DataSet
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = "SELECT * FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
// fill the dataset and retrieve the default view
da.Fill(ds, "Employees");
DataView dv = ds.Tables["Employees"].DefaultView;
// sort the view according to the value stored in the ViewState, in any
if (ViewState["SortExpression"] != null)
dv.Sort = ViewState["SortExpression"].ToString(); Datagrid1.DataSource = dv;
Datagrid1.DataBind();
}
protected void Datagrid1_Sort(object sender, DataGridSortCommandEventArgs e)
{
ViewState["SortExpression"] = e.SortExpression;
Datagrid1.SelectedIndex = -1;
BindGrid();
}
protected void Datagrid1_SelectionChanged(object sender, EventArgs e)
{
textbox1.Text = Datagrid1.SelectedItem.Cells[2].Text.ToString();
BindGrid();
}
}
}
DG的ItemCommand在那呀,我咋没有找到
private void CallCurrentCellChanged()
{
myDataGrid.CurrentCellChanged += new EventHandler(Grid_CurCellChange);
}// Raise the event when focus on DataGrid cell changes.
protected void Grid_CurCellChange(object sender, EventArgs e)
{
// String variable used to show message.
string myString = "CurrentCellChanged event raised, cell focus is at ";
// Get the co-ordinates of the focussed cell.
string myPoint = myDataGrid.CurrentCell.ColumnNumber + "," +
myDataGrid.CurrentCell.RowNumber;
// Create the alert message.
myString = myString + "(" + myPoint + ")";
// Show Co-ordinates when CurrentCellChanged event is raised.
MessageBox.Show(myString, "Current cell co-ordinates");
}
System.EventArgs e)
{
MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber
+ ", Row is " + myDataGrid.CurrentCell.RowNumber
+ ", Value is " + myDataGrid[myDataGrid.CurrentCell]//获得CurrentCell的值
);
}