使用GridView控件完成修改、删除、保存功能示例
//GridView设置功能选项
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" PageSize="4"
style="font-size: small" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging">
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="BccdID" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="BccdName" HeaderText="商品名称" />
<asp:BoundField DataField="BccdPrice" HeaderText="商品价格" />
<asp:BoundField DataField="BccdSaleDate" DataFormatString="{0:d}"
HeaderText="发放日期" />
<asp:BoundField DataField="BccdInStock" HeaderText="现有库存量" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField ButtonType="Image" CancelImageUrl="~/Images/BtnCancel.gif"
EditImageUrl="~/Images/BtnUpdate.gif" HeaderText="编辑" ShowEditButton="True" //修改按钮图像BtnUpdate.gif
UpdateImageUrl="~/Images/BtnSave.gif" />//保存按钮图像BtnSave.gif
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete"
ImageUrl="~/Images/BtnDelete.gif" //删除按钮图像
onclientclick="return confirm('确定删除吗?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
//处理事件程序
//定义加载查询数据结果
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();//调用自定义方法绑定数据到控件
}
}//CodeGo.net/
public void BindData()
{
string strCon = ConfigurationManager.AppSettings["conStr"];//定义数据库连接字符串
string sqlstr = "select * from mrbccd";//定义执行查询操作的SQL语句
SqlConnection con = new SqlConnection(strCon);//创建数据库连接对象
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);//创建数据适配器
DataSet ds = new DataSet();//创建数据集
da.Fill(ds); //填充数据集
GridView1.DataSource = ds;//设置GridView控件的数据源为创建的数据集ds
//将数据库表中的主键字段放入GridView控件的DataKeyNames属性中
GridView1.DataKeyNames = new string[] { "BccdID" };
GridView1.DataBind();//绑定数据库表中数据
}
//逐条显示数据将编辑的数据并进行绑定
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();//数据绑定
}
//更新数据库修改数据重新绑定数据内容
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//取得编辑行的关键字段的值
string bccdID = GridView1.DataKeys[e.RowIndex].Value.ToString();
//取得文本框中输入的内容
string bccdName=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string bccdPrice=((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string bccdSaleDate=((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string bccdInStock=((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
//定义更新操作的SQL语句
string update_sql = "update mrbccd set BccdName='" + bccdName + "',BccdPrice='" + bccdPrice + "',bccdSaleDate='" + Convert.ToDateTime(bccdSaleDate) + "',BccdInStock='" + bccdInStock + "' where BccdID='" + bccdID + "'";
bool update = ExceSQL(update_sql);//调用ExceSQL执行更新操作
if (update)
{
Response.Write("<script language=javascript>alert('修改成功!')</script>");
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
BindData();
}
else
{
Response.Write("<script language=javascript>alert('修改失败!');</script>");
}
}
//删除数据事件(省略)
//GridView设置功能选项
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" PageSize="4"
style="font-size: small" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging">
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="BccdID" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="BccdName" HeaderText="商品名称" />
<asp:BoundField DataField="BccdPrice" HeaderText="商品价格" />
<asp:BoundField DataField="BccdSaleDate" DataFormatString="{0:d}"
HeaderText="发放日期" />
<asp:BoundField DataField="BccdInStock" HeaderText="现有库存量" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField ButtonType="Image" CancelImageUrl="~/Images/BtnCancel.gif"
EditImageUrl="~/Images/BtnUpdate.gif" HeaderText="编辑" ShowEditButton="True" //修改按钮图像BtnUpdate.gif
UpdateImageUrl="~/Images/BtnSave.gif" />//保存按钮图像BtnSave.gif
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete"
ImageUrl="~/Images/BtnDelete.gif" //删除按钮图像
onclientclick="return confirm('确定删除吗?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
//处理事件程序
//定义加载查询数据结果
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();//调用自定义方法绑定数据到控件
}
}//CodeGo.net/
public void BindData()
{
string strCon = ConfigurationManager.AppSettings["conStr"];//定义数据库连接字符串
string sqlstr = "select * from mrbccd";//定义执行查询操作的SQL语句
SqlConnection con = new SqlConnection(strCon);//创建数据库连接对象
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);//创建数据适配器
DataSet ds = new DataSet();//创建数据集
da.Fill(ds); //填充数据集
GridView1.DataSource = ds;//设置GridView控件的数据源为创建的数据集ds
//将数据库表中的主键字段放入GridView控件的DataKeyNames属性中
GridView1.DataKeyNames = new string[] { "BccdID" };
GridView1.DataBind();//绑定数据库表中数据
}
//逐条显示数据将编辑的数据并进行绑定
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();//数据绑定
}
//更新数据库修改数据重新绑定数据内容
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//取得编辑行的关键字段的值
string bccdID = GridView1.DataKeys[e.RowIndex].Value.ToString();
//取得文本框中输入的内容
string bccdName=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string bccdPrice=((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string bccdSaleDate=((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string bccdInStock=((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
//定义更新操作的SQL语句
string update_sql = "update mrbccd set BccdName='" + bccdName + "',BccdPrice='" + bccdPrice + "',bccdSaleDate='" + Convert.ToDateTime(bccdSaleDate) + "',BccdInStock='" + bccdInStock + "' where BccdID='" + bccdID + "'";
bool update = ExceSQL(update_sql);//调用ExceSQL执行更新操作
if (update)
{
Response.Write("<script language=javascript>alert('修改成功!')</script>");
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
BindData();
}
else
{
Response.Write("<script language=javascript>alert('修改失败!');</script>");
}
}
//删除数据事件(省略)
解决方案 »
- c#怎么赋值给num1,num2,具体数字
- 当双击ListView控件时,怎样获得双击的是那个Item
- 一个调水问题,本人菜鸟,求高手帮助.
- $$$$$$$$$$$$$$$$$$$$怎么会有这样的题目!!!!!!!!!!只有神仙可以理解。。。。。。。。郁闷。。。。。
- C#编写的珠宝软件,请多指教。
- DataTable和OleDbDataAdapter的问题,100高分请教!!!!!!
- c#三层架构问题,高手请进
- 程序向导的小问题
- C#中用outlook2003发电子邮件,如何解除安全警告提示????
- C#多线程并发(请问一下这个是不是线程的并发啊?)
- winform 浮动窗体图片毛边
- 求一正则表达式
获取页面数据:
string bccdName=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
拼接sql 或者存储过程
string update_sql = "update mrbccd set BccdName='" + bccdName + "',BccdPrice='" + bccdPrice + "',bccdSaleDate='" + Convert.ToDateTime(bccdSaleDate) + "',BccdInStock='" + bccdInStock + "' where BccdID='" + bccdID + "'";
写入数据库
ExceSQL(update_sql);//调用ExceSQL执行更新操作
???????是不是还要引用什么类型啊