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 , 自己写代码,
拜托。 半个小时。。。。 麻烦各位。
展开
 我来答
最后的仙人
2010-06-15 · TA获得超过1961个赞
知道大有可为答主
回答量:1509
采纳率:0%
帮助的人:1398万
展开全部
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();
}
}
cultsharp
2010-06-14 · 超过26用户采纳过TA的回答
知道答主
回答量:91
采纳率:0%
帮助的人:71.8万
展开全部
一个简单的方法是直接用gridview自带的数据绑定就行了,添加一个sqldatasource, 然后在aspx中的sqldatasource中定义updatecommand和deletecommand就行了
例如:
updateCommand=“update users set ....”;
deleteCommand="delete from where name=@name";
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
冷檬聊电影
2010-06-14 · TA获得超过307个赞
知道小有建树答主
回答量:567
采纳率:0%
帮助的人:255万
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友133cb66fa
2010-06-14 · TA获得超过324个赞
知道小有建树答主
回答量:692
采纳率:0%
帮助的人:386万
展开全部
恩对就是不给他代码,,只给他说思路..
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式