一。数据显示
1。分页
(1)DataGrid控件属性AllowPaging=true
或使用Property Builder工具进行配置(右击DataGrid控件-paging)
(2)添加事件处理程序
双击PageIndexChanged属性,生成函数
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex; //改变当前页参数
DataGrid1.SelectedIndex=-1; //改变页时,清空上页所选参数
BindDataGrid(); //重新绑定数据
}
2。选择行
(1)将记录的索引字段赋予DataGrid,以便后面定位之用
DataGrid1.DataSource=dataSet;
DataGrid1.DataKeyField="EmployeeID"; //
DataGrid1.DataBind();
(2)创建ButtonColumn,可使用Property Builder工具
<asp:DataGrid>
<Columns>
<asp:ButtonColumn Text="选择" CommandName="Select" />
(3)双击SelectedIndexChange,添加事件,生成函数
public void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//根据所选记录的Index,得到索引字段的内容,作为select的条件参数
string key=DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();
string sqlQuery="Select × From Employees Where EmployeeID=@EmployeeID";
sqlConnection1.Open();
SqlCommand sqlCommand=new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
。。。。。。。//用DataSet/DataReader等获取数据,供使用(赋给其他控件等)
sqlConnection1.Close();
}
3。使用绑定列,自我指定要显示的列
(1)AutoGeneratedColumns=false,
(2)并创建绑定列BoundColumn,赋予其要显示的字段
<asp:BoundColumn DataField="FirstName" HeaderText="First Name" />
二。数据编辑
1。添加编辑
(1)创建EditCommandColumn
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="取消" EditText="编辑">
(2)双击EditCommand属性,生成函数
//点击EditCommandColumn,进入editText 引发事件
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex; //使所选行进入编辑状态
BindDataGrid();
}
(3)双击CancelCommand属性,生成函数
//点击EditCommandColumn,进入CancelText 引发事件
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1; //使所选行恢复非编辑状态
BindDataGrid();
}
(4)双击UpdateCommand属性,生成函数
//点击EditCommandColumn,进入updateText 引发事件
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//提取处于编辑状态的行的索引字段,即EmployeeID
string key=DataGrid1.DataKeys[DataGrid1.EditItemIndex].ToString();
string sqlQuery="Update Employees Set FirstName=@FirstName Where EmployeeID=@EmployeeID";
sqlConnection1.Open();
SqlCommand sqlCommand=new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
//提取目前所选item的textBox控件内的内容。作为参数内容
sqlCommand.Parameters.Add("@FirstName",((TextBox)(e.Item.Cells[1].Controls[0])).Text);
sqlCommand.ExecuteNonQuery();
sqlConnection1.Close();
DataGrid1.EditItemIndex=-1; //恢复未编辑状态
BindDataGrid();
}
2。使用模板列
<Columns>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.HireDate", "{0:d}") %>' />
</ItemTemplate>
3.删除行
(1)创建BoundColumn
<asp:ButtonColumn Text="删除" CommandName="Delete" />
(2)双击DeleteCommand,生成函数
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string key=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
//提取选中行的索引字段,即EmployeeID
string sqlQuery="Delete Employees Where EmployeeID=@EmployeeID";
sqlConnection1.Open();
SqlCommand sqlCommand=new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
sqlCommand.ExecuteNonQuery();
sqlConnection1.Close();
DataGrid1.SelectedIndex=-1;
DataGrid1.EditItemIndex=-1; //恢复未编辑状态
BindDataGrid();
}
浙公网安备 33010602011771号