数据控件

您所在的位置:网站首页 Label控件名词解释 数据控件

数据控件

2024-07-10 14:12| 来源: 网络整理| 查看: 265

1.GridView 数据源控件成功绑定查询数据之后,就必须将查询数据显示出来。 第一种 VS中进行简单设置来实现 第二种 动态数据绑定之显示查询数据

1.就是通过DataAdapter对象进行数据查询 2.并把数据填充到DataTable对象。 3.把DataTable对象设置为GridView控件的数据源 4.DataBind()绑定 protect void Button1_Click(object sender, EventArgs e)//查询按钮 { Session["data1"] = TextBox1.Text; Session["data2"] = TextBox2.Text; dataBin(); } void dataBin() { SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString); conn.Open(); string sql_str = "SELECT Orders.OrderID,Orders.CustomerID," + "Customers.Company,Orders.EmployeeID," + "Employee.LastName + '.' + Employee.FirstName AS Employee," + "Orders.OrderDate FROM Orders INNER JOIN" + "Customers ON Orders.EmployeeID = Employees.EmployeeID "+ "WHERE (Orders.OrderData BETWEEN '"+ Session["data1"].ToString() +"'AND '"Session["date2"].ToString() + "')"; SqlDataAdapter sqladp = new SqlDataAdapter(sql_str,conn);//创建SqlDataAdapter对象实例 DataTable dt = new DataTable(); sqladp.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); }

动态数据绑定之操作数据的功能实现 通过事件来实现编辑,删除,排序,分页等功能。 https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowediting?view=netframework-4.8

protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex();//设置当前行为编辑行 dataBin(); } protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEventArgs e) { GridView1.EditIndex = -1;//设置当前编辑行为空 dataBin(); }

例子 实现可以更新数据的功能按钮。添加RowUpdating事件

string CustomerID_str; DropDownList Cu_drop = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1"); CustomerID_str = Cu_drop.SelectedValue.ToString(); string EmployeeID_str; DropDownList Em_drop = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2"); EmployeeID_str = Em_drop.SelectedValue.ToString(); string OrderData_str; TextBox Order_Box =(TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1"); OrderData_str = Order_Box.Text; string OrderID_str = GridView1.DataKeys.[e.RowIndex].Valus.ToString(); string Update_sql = "UPDATE [Orders] SET [OrderDate] = '" + OrderData_str + "',//定义UPDATE语句 [EmployeeID] = " + EmployeeID_str +", [CustomerID] = '"CustomerID_str+"' WHERE [OrderID_str] = "+OrderID_str+ " "; SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString); try { conn.Open(); SqlCommand cmm = new SqlCommand(); cmm.CommandText = Update_sql; cmm.Connection = connl cmm.ExecuteNonQuery();//执行SqlCommand对象 conn.Close(); } catch(Exception ex) { Response.Write("ex.Message"); } GridView1.EditIndex = -1;//取消编辑状态 dataBin();

例子 实现可以删除数据的功能按钮。添加RowDelteting事件 在这里插入图片描述

string OrderID_str = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取订单编号 string Delete_str = "Delete From Orders WHERE [OrderID] =" + OrderID_str + ""; SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString); try { conn.Open(); SqlCommand cmm = new SqlCommand(); cmm.CommandText = Delete_sql; cmm.Connection = connl cmm.ExecuteNonQuery();//执行SqlCommand对象 conn.Close(); } catch(Exception ex) { Response.Write("ex.Message"); } GridView1.EditIndex = -1;//取消编辑状态 dataBin();

2.DataList 一般用于显示只读的查询数据。该控件可以分列显示数据。常常运用于产品显示。本身是不支持分页的,但通过PagedDataSource对象可以实现分页功能。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3