DataGrid与DataSet绑定后,对于DataSet中状态为Deleted的行不显示,这样一来,DataGrid中的行编号与DataSet中的行编号就可能不相同,例如DataGrid中的第一行可能显示的是DataSet中第二行的数据。那么如何获得DataGrid某行对应DataSet中的哪一行??DataSetIndex属性好像没用,和ItemIndex值是一样的呀,谢谢
解决方案 »
- 问一个关于发布的问题:为什么我的右键没有发布选项呢
- jQuery ajax无法阻止事件触发,急!!!怎么办!!!求教。。。
- 如何動態的隱藏gridview中模板的某一列?
- .请问 html table与table.document有什么区别?
- window关闭问题!
- ---------------------------DataGrid样式一问,求教---------------------------
- 急急。。
- asp.net 怎么去掉后缀名.aspx 详细方案或者是案例
- CompositeControl和ParseChildren(true, "Employees")
- 各位大侠,我的水晶报表怎么调用的时候就不行了呢?(提示我没有获得免费的版本)
- 'document.input.message'为空或不是对象!!!!!!!
- 有关"未将对象引用设置到对象的实例"的错误
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><html><head> <script runat="server"> ICollection CreateDataSource()
{
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double))); // Define the primary key for the table as the IntegerValue
// column (column 0). To do this, first create an array of
// DataColumns to represent the primary key. The primary key can
// consist of multiple columns, but in this example, only
// one column is used.
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns[0]; // Then assign the array to the PrimaryKey property of the DataTable.
dt.PrimaryKey = keys;
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
} // To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
DataView dv = new DataView(dt);
return dv; }
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
} } void Delete_Command(Object sender, DataGridCommandEventArgs e)
{ // Retrieve the data table from session state.
DataTable dt = (DataTable)Session["Source"]; // Retrieve the data row to delete from the data table.
// Use the DataKeys property of the DataGrid control to get
// the primary key value of the selected row.
// Search the Rows collection of the data table for this value.
DataRow row;
row = dt.Rows.Find(ItemsGrid.DataKeys[e.Item.ItemIndex]); // Delete the item selected in the DataGrid from the data source.
if(row != null)
{
dt.Rows.Remove(row);
} // Save the data source.
Session["Source"] = dt; // Create a DataView and bind it to the DataGrid control.
DataView dv = new DataView(dt);
ItemsGrid.DataSource = dv;
ItemsGrid.DataBind(); } </script></head><body> <form runat="server"> <h3>BaseDataList DataKeys Example</h3> <asp:DataGrid id="ItemsGrid"
BorderColor="Black"
ShowFooter="False"
CellPadding=3
CellSpacing="0"
HeaderStyle-BackColor="#aaaadd"
DataKeyField="IntegerValue"
OnDeleteCommand="Delete_Command"
runat="server"> <Columns> <asp:ButtonColumn Text="Delete"
CommandName="Delete"/> </Columns> </asp:DataGrid> </form></body>
</html>