asp.net数据控件gridview
我没时间,想请个人,帮我做一个简单的东西,就是用gridview绑定数据,并实现gridview自带的修改与删除功能即可,,要求用sqlsever2005数据库,字段名,...
我没时间,想请个人,帮我做一个简单的东西,就是用gridview绑定数据,并实现gridview自带的修改与删除功能即可,,
要求用 sqlsever2005数据库,字段名,按顺序是:name,pwd,sex,age,mail,time。表名就叫users,其中,pwd和time两个字段不显示,
就一个简单的东西,
麻烦各位,若能在2010年6月16日之前给我,即可送上100分为谢。
qq 422392557
不用sqldatasource , 自己写代码,
拜托。 半个小时。。。。 麻烦各位。 展开
要求用 sqlsever2005数据库,字段名,按顺序是:name,pwd,sex,age,mail,time。表名就叫users,其中,pwd和time两个字段不显示,
就一个简单的东西,
麻烦各位,若能在2010年6月16日之前给我,即可送上100分为谢。
qq 422392557
不用sqldatasource , 自己写代码,
拜托。 半个小时。。。。 麻烦各位。 展开
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (DropDownList1.Items.Count == 1)
{
AddItems();
}
if (!this.IsPostBack)
{
BindGridView();
}
if (this.IsPostBack)
{
AddControls();
}
}
SqlConnection sqlcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;Integrated Security=True");
protected void AddItems()
{
SqlCommand comm = new SqlCommand("select * from sysobjects where xtype='U'", sqlcon);
SqlDataAdapter sqlda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sqlda.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
DropDownList1.Items.Add(row[0].ToString());
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindGridView();
}
protected void BindGridView()
{
if (DropDownList1.SelectedIndex == 0)
{
GridView1.DataSource = null;
GridView1.DataBind();
Button2.Visible = false;
Label2.Visible = false;
}
else
{
GridView1.Columns.Clear();
DataSet ds = getDataSource();
foreach (DataColumn item in ds.Tables[0].Columns)
{
BoundField col = new BoundField();
col.HeaderText = item.ColumnName;
col.DataField = item.ColumnName;
col.Visible = true;
GridView1.Columns.Add(col);
}
CommandField links = new CommandField();
links.HeaderText = "操作";
links.ShowEditButton = true;
links.ShowDeleteButton = true;
links.ShowSelectButton = true;
GridView1.Columns.Add(links);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
Label2.Visible = true;
Button2.Visible = true;
AddControls();
}
}
protected DataSet getDataSource()
{
SqlCommand comm = new SqlCommand(@"select * from " + DropDownList1.SelectedItem.Text, sqlcon);
SqlDataAdapter sqlda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sqlda.Fill(ds);
return ds;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
deleteRow(e.RowIndex);
BindGridView();
}
protected void deleteRow(int rowindex)
{
String tablename = DropDownList1.SelectedItem.Text;
String columnname = GridView1.Columns[0].HeaderText;
String id = GridView1.Rows[rowindex].Cells[0].Text;
String command = "delete from " + tablename + " where " + columnname + "='" + id + "'";
SqlCommand comm = new SqlCommand(command, sqlcon);
sqlcon.Open();
comm.ExecuteNonQuery();
sqlcon.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[GridView1.Columns.Count - 1].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('你确定要删除该项吗?')");
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String idname = GridView1.Columns[0].HeaderText;
String id = Label1.Text;
List<String> values = new List<string>();
List<String> columns = new List<string>();
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
values.Add((GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text);
columns.Add(GridView1.Columns[i].HeaderText);
}
String tablename = DropDownList1.SelectedItem.Text;
String commandText = "update " + tablename + " set ";
for (int i = 0; i < GridView1.Columns.Count - 3; i++)
{
commandText += columns[i] + "='" + values[i] + "',";
}
commandText += columns[GridView1.Columns.Count - 3] + "='" + values[GridView1.Columns.Count - 3] + "' where " + idname + "='" + id + "'";
SqlCommand comm = new SqlCommand(commandText, sqlcon);
try
{
sqlcon.Open();
comm.ExecuteNonQuery();
comm.Dispose();
sqlcon.Close();
GridView1.EditIndex = -1;
BindGridView();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void AddControls()
{
this.Panel1.Controls.Clear();
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
Label lbl = new Label();
lbl.Text = GridView1.Columns[i].HeaderText;
TextBox tb = new TextBox();
tb.ID = "TextBox" + i.ToString();
this.Panel1.Controls.Add(lbl);
this.Panel1.Controls.Add(tb);
}
}
public const String message = "Cannot Insert Blank into Database!";
protected void button_Click(object sender, EventArgs e)
{
List<String> columns = new List<string>();
List<String> text = new List<string>();
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
columns.Add(GridView1.Columns[i].HeaderText);
}
int length = 0;
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
text.Add(((TextBox)this.FindControl("TextBox" + i.ToString())).Text.Trim());
length += text[i - 1].Length;
}
if (length == 0)
{
return;
}
String tablename = DropDownList1.SelectedItem.Text;
String commandText = "insert into " + tablename + " (";
for (int i = 0; i < GridView1.Columns.Count - 3; i++)
{
commandText += columns[i] + ",";
}
commandText += columns[GridView1.Columns.Count - 3] + ") values('";
for (int i = 0; i < GridView1.Columns.Count - 3; i++)
{
commandText += text[i] + "','";
}
commandText += text[GridView1.Columns.Count - 3] + "')";
SqlCommand comm = new SqlCommand(commandText, sqlcon);
try
{
sqlcon.Open();
comm.ExecuteNonQuery();
sqlcon.Close();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
BindGridView();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = 0;
index = GridView1.SelectedIndex;
string str_SalesOrg = GridView1.Rows[index].Cells[0].Text.Trim();
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (DropDownList1.Items.Count == 1)
{
AddItems();
}
if (!this.IsPostBack)
{
BindGridView();
}
if (this.IsPostBack)
{
AddControls();
}
}
SqlConnection sqlcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;Integrated Security=True");
protected void AddItems()
{
SqlCommand comm = new SqlCommand("select * from sysobjects where xtype='U'", sqlcon);
SqlDataAdapter sqlda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sqlda.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
DropDownList1.Items.Add(row[0].ToString());
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindGridView();
}
protected void BindGridView()
{
if (DropDownList1.SelectedIndex == 0)
{
GridView1.DataSource = null;
GridView1.DataBind();
Button2.Visible = false;
Label2.Visible = false;
}
else
{
GridView1.Columns.Clear();
DataSet ds = getDataSource();
foreach (DataColumn item in ds.Tables[0].Columns)
{
BoundField col = new BoundField();
col.HeaderText = item.ColumnName;
col.DataField = item.ColumnName;
col.Visible = true;
GridView1.Columns.Add(col);
}
CommandField links = new CommandField();
links.HeaderText = "操作";
links.ShowEditButton = true;
links.ShowDeleteButton = true;
links.ShowSelectButton = true;
GridView1.Columns.Add(links);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
Label2.Visible = true;
Button2.Visible = true;
AddControls();
}
}
protected DataSet getDataSource()
{
SqlCommand comm = new SqlCommand(@"select * from " + DropDownList1.SelectedItem.Text, sqlcon);
SqlDataAdapter sqlda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sqlda.Fill(ds);
return ds;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
deleteRow(e.RowIndex);
BindGridView();
}
protected void deleteRow(int rowindex)
{
String tablename = DropDownList1.SelectedItem.Text;
String columnname = GridView1.Columns[0].HeaderText;
String id = GridView1.Rows[rowindex].Cells[0].Text;
String command = "delete from " + tablename + " where " + columnname + "='" + id + "'";
SqlCommand comm = new SqlCommand(command, sqlcon);
sqlcon.Open();
comm.ExecuteNonQuery();
sqlcon.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[GridView1.Columns.Count - 1].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('你确定要删除该项吗?')");
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String idname = GridView1.Columns[0].HeaderText;
String id = Label1.Text;
List<String> values = new List<string>();
List<String> columns = new List<string>();
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
values.Add((GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text);
columns.Add(GridView1.Columns[i].HeaderText);
}
String tablename = DropDownList1.SelectedItem.Text;
String commandText = "update " + tablename + " set ";
for (int i = 0; i < GridView1.Columns.Count - 3; i++)
{
commandText += columns[i] + "='" + values[i] + "',";
}
commandText += columns[GridView1.Columns.Count - 3] + "='" + values[GridView1.Columns.Count - 3] + "' where " + idname + "='" + id + "'";
SqlCommand comm = new SqlCommand(commandText, sqlcon);
try
{
sqlcon.Open();
comm.ExecuteNonQuery();
comm.Dispose();
sqlcon.Close();
GridView1.EditIndex = -1;
BindGridView();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void AddControls()
{
this.Panel1.Controls.Clear();
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
Label lbl = new Label();
lbl.Text = GridView1.Columns[i].HeaderText;
TextBox tb = new TextBox();
tb.ID = "TextBox" + i.ToString();
this.Panel1.Controls.Add(lbl);
this.Panel1.Controls.Add(tb);
}
}
public const String message = "Cannot Insert Blank into Database!";
protected void button_Click(object sender, EventArgs e)
{
List<String> columns = new List<string>();
List<String> text = new List<string>();
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
columns.Add(GridView1.Columns[i].HeaderText);
}
int length = 0;
for (int i = 1; i < GridView1.Columns.Count - 1; i++)
{
text.Add(((TextBox)this.FindControl("TextBox" + i.ToString())).Text.Trim());
length += text[i - 1].Length;
}
if (length == 0)
{
return;
}
String tablename = DropDownList1.SelectedItem.Text;
String commandText = "insert into " + tablename + " (";
for (int i = 0; i < GridView1.Columns.Count - 3; i++)
{
commandText += columns[i] + ",";
}
commandText += columns[GridView1.Columns.Count - 3] + ") values('";
for (int i = 0; i < GridView1.Columns.Count - 3; i++)
{
commandText += text[i] + "','";
}
commandText += text[GridView1.Columns.Count - 3] + "')";
SqlCommand comm = new SqlCommand(commandText, sqlcon);
try
{
sqlcon.Open();
comm.ExecuteNonQuery();
sqlcon.Close();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
BindGridView();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = 0;
index = GridView1.SelectedIndex;
string str_SalesOrg = GridView1.Rows[index].Cells[0].Text.Trim();
}
}
展开全部
一个简单的方法是直接用gridview自带的数据绑定就行了,添加一个sqldatasource, 然后在aspx中的sqldatasource中定义updatecommand和deletecommand就行了
例如:
updateCommand=“update users set ....”;
deleteCommand="delete from where name=@name";
例如:
updateCommand=“update users set ....”;
deleteCommand="delete from where name=@name";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
gridview大全 里面有你要的功能
http://hi.baidu.com/wenjunlin/blog/item/dfbad1dd61008f385982dd8e.html
http://hi.baidu.com/wenjunlin/blog/item/dfbad1dd61008f385982dd8e.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
恩对就是不给他代码,,只给他说思路..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询