10 个解决方案
#1
.net的本意是三层结构,不希望直接操作数据库!
所以你的问题的解决办法我想还是SQL SERVER--DATASET---DATAGRID。
所以你的问题的解决办法我想还是SQL SERVER--DATASET---DATAGRID。
#2
我现在是在WebForm中放了DATAGRID控件,连接了SQL数据库,还有DATASET,设置了查询的条件。也在WebForm1.aspx.cn中写好了代码。可以实现查询功能。但在同一窗体下删除,修改,添加的功能要如何实现呢?
#3
我本来是想在Windows应用程序中那样,用TEXTBOX来显示数据库内容。这样修改,保存就容易。但在WEB中怎样将TEXTBOX与数据库绑定不知道啊。就是想一运行程序就可以在TEXTBOX中显示数据库内容。然后逐一实现上一记录,下一记录,增加,删除,修改等功能。请高手指点。
#4
.net没有powerbulider那么 容易 不是一个语句 就删除 添加了 ..
#5
看看数据库连接方面的书 ado.net连接数据库
#6
用膜板列做,在DATAGRID的事件里面写,比如删除
private void dgQuestion_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int isDel = Convert.ToInt32(e.Item.Cells[0].Text);
bool bl = busi.delQuestion(isDel); //这里调用逻辑层的方法
if(bl == true)
{
if(dgQuestion.CurrentPageIndex == dgQuestion.PageCount - 1)
{
if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
if(dgQuestion.Items.Count % dgQuestion.PageSize == 1)
{
dgQuestion.CurrentPageIndex = dgQuestion.PageCount - 2;
Bind();
}
else
{
Bind();
}
}
}
else if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
Bind();
}
}
}
Bind()是自定义的方法,不管,我做的是分页+删除+修改,用三层实现的
private void dgQuestion_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int isDel = Convert.ToInt32(e.Item.Cells[0].Text);
bool bl = busi.delQuestion(isDel); //这里调用逻辑层的方法
if(bl == true)
{
if(dgQuestion.CurrentPageIndex == dgQuestion.PageCount - 1)
{
if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
if(dgQuestion.Items.Count % dgQuestion.PageSize == 1)
{
dgQuestion.CurrentPageIndex = dgQuestion.PageCount - 2;
Bind();
}
else
{
Bind();
}
}
}
else if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
Bind();
}
}
}
Bind()是自定义的方法,不管,我做的是分页+删除+修改,用三层实现的
#7
DataGrid_itemcommand 事件
#8
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//得到新的页索引
int ipos =e.NewPageIndex;
//设置当前页
this.DataGrid1.CurrentPageIndex=ipos;
//更新页面
DataSet MySet = new DataSet() ;
this.oleDbDataAdapter1.Fill(MySet) ;
this.DataGrid1.DataSource=MySet;
this.DataBind();
}但无法实现分页。是不是要在private void InitializeComponent()中有对PageIndexChanged的定义啊,要怎么定义呢?
{
//得到新的页索引
int ipos =e.NewPageIndex;
//设置当前页
this.DataGrid1.CurrentPageIndex=ipos;
//更新页面
DataSet MySet = new DataSet() ;
this.oleDbDataAdapter1.Fill(MySet) ;
this.DataGrid1.DataSource=MySet;
this.DataBind();
}但无法实现分页。是不是要在private void InitializeComponent()中有对PageIndexChanged的定义啊,要怎么定义呢?
#9
你老了?
给你一段代码?
你看看。。。。。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MsDataGrid
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class UseDropDown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgShow;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
BindData();
}
private void BindData()
{
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
DataSet ds = new DataSet();
da.Fill(ds,"studentinfo");
dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
dgShow.DataBind();
foreach(DataGridItem dgi in dgShow.Items)
{
//以下绑定非编辑状态下拉列表
DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
if(ddI!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddI.SelectedIndex = 0;
else
ddI.SelectedIndex = 1;
}
//以下绑定编辑状态下拉列表
DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
if(ddE!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddE.SelectedIndex = 0;
else
ddE.SelectedIndex = 1;
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();
}
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex = e.NewPageIndex;
BindData();
}
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
////////////////////////////////////////////////////////////
//说明:执行制定SQL语句/////////////////////////////////////
///////////////////////////////////////////////////////////
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(Exception e)
{
Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
}
}
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((DropDownList)(e.Item.FindControl("ddlSexE"))).SelectedItem.Value;
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();
}
}
}
给你一段代码?
你看看。。。。。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MsDataGrid
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class UseDropDown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgShow;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
BindData();
}
private void BindData()
{
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
DataSet ds = new DataSet();
da.Fill(ds,"studentinfo");
dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
dgShow.DataBind();
foreach(DataGridItem dgi in dgShow.Items)
{
//以下绑定非编辑状态下拉列表
DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
if(ddI!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddI.SelectedIndex = 0;
else
ddI.SelectedIndex = 1;
}
//以下绑定编辑状态下拉列表
DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
if(ddE!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddE.SelectedIndex = 0;
else
ddE.SelectedIndex = 1;
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();
}
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex = e.NewPageIndex;
BindData();
}
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
////////////////////////////////////////////////////////////
//说明:执行制定SQL语句/////////////////////////////////////
///////////////////////////////////////////////////////////
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(Exception e)
{
Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
}
}
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((DropDownList)(e.Item.FindControl("ddlSexE"))).SelectedItem.Value;
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();
}
}
}
#10
不行的话,
qq:329295182
qq:329295182
#1
.net的本意是三层结构,不希望直接操作数据库!
所以你的问题的解决办法我想还是SQL SERVER--DATASET---DATAGRID。
所以你的问题的解决办法我想还是SQL SERVER--DATASET---DATAGRID。
#2
我现在是在WebForm中放了DATAGRID控件,连接了SQL数据库,还有DATASET,设置了查询的条件。也在WebForm1.aspx.cn中写好了代码。可以实现查询功能。但在同一窗体下删除,修改,添加的功能要如何实现呢?
#3
我本来是想在Windows应用程序中那样,用TEXTBOX来显示数据库内容。这样修改,保存就容易。但在WEB中怎样将TEXTBOX与数据库绑定不知道啊。就是想一运行程序就可以在TEXTBOX中显示数据库内容。然后逐一实现上一记录,下一记录,增加,删除,修改等功能。请高手指点。
#4
.net没有powerbulider那么 容易 不是一个语句 就删除 添加了 ..
#5
看看数据库连接方面的书 ado.net连接数据库
#6
用膜板列做,在DATAGRID的事件里面写,比如删除
private void dgQuestion_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int isDel = Convert.ToInt32(e.Item.Cells[0].Text);
bool bl = busi.delQuestion(isDel); //这里调用逻辑层的方法
if(bl == true)
{
if(dgQuestion.CurrentPageIndex == dgQuestion.PageCount - 1)
{
if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
if(dgQuestion.Items.Count % dgQuestion.PageSize == 1)
{
dgQuestion.CurrentPageIndex = dgQuestion.PageCount - 2;
Bind();
}
else
{
Bind();
}
}
}
else if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
Bind();
}
}
}
Bind()是自定义的方法,不管,我做的是分页+删除+修改,用三层实现的
private void dgQuestion_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int isDel = Convert.ToInt32(e.Item.Cells[0].Text);
bool bl = busi.delQuestion(isDel); //这里调用逻辑层的方法
if(bl == true)
{
if(dgQuestion.CurrentPageIndex == dgQuestion.PageCount - 1)
{
if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
if(dgQuestion.Items.Count % dgQuestion.PageSize == 1)
{
dgQuestion.CurrentPageIndex = dgQuestion.PageCount - 2;
Bind();
}
else
{
Bind();
}
}
}
else if(dgQuestion.CurrentPageIndex == 0)
{
Bind();
}
else
{
Bind();
}
}
}
Bind()是自定义的方法,不管,我做的是分页+删除+修改,用三层实现的
#7
DataGrid_itemcommand 事件
#8
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//得到新的页索引
int ipos =e.NewPageIndex;
//设置当前页
this.DataGrid1.CurrentPageIndex=ipos;
//更新页面
DataSet MySet = new DataSet() ;
this.oleDbDataAdapter1.Fill(MySet) ;
this.DataGrid1.DataSource=MySet;
this.DataBind();
}但无法实现分页。是不是要在private void InitializeComponent()中有对PageIndexChanged的定义啊,要怎么定义呢?
{
//得到新的页索引
int ipos =e.NewPageIndex;
//设置当前页
this.DataGrid1.CurrentPageIndex=ipos;
//更新页面
DataSet MySet = new DataSet() ;
this.oleDbDataAdapter1.Fill(MySet) ;
this.DataGrid1.DataSource=MySet;
this.DataBind();
}但无法实现分页。是不是要在private void InitializeComponent()中有对PageIndexChanged的定义啊,要怎么定义呢?
#9
你老了?
给你一段代码?
你看看。。。。。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MsDataGrid
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class UseDropDown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgShow;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
BindData();
}
private void BindData()
{
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
DataSet ds = new DataSet();
da.Fill(ds,"studentinfo");
dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
dgShow.DataBind();
foreach(DataGridItem dgi in dgShow.Items)
{
//以下绑定非编辑状态下拉列表
DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
if(ddI!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddI.SelectedIndex = 0;
else
ddI.SelectedIndex = 1;
}
//以下绑定编辑状态下拉列表
DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
if(ddE!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddE.SelectedIndex = 0;
else
ddE.SelectedIndex = 1;
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();
}
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex = e.NewPageIndex;
BindData();
}
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
////////////////////////////////////////////////////////////
//说明:执行制定SQL语句/////////////////////////////////////
///////////////////////////////////////////////////////////
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(Exception e)
{
Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
}
}
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((DropDownList)(e.Item.FindControl("ddlSexE"))).SelectedItem.Value;
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();
}
}
}
给你一段代码?
你看看。。。。。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MsDataGrid
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class UseDropDown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgShow;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
BindData();
}
private void BindData()
{
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
DataSet ds = new DataSet();
da.Fill(ds,"studentinfo");
dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
dgShow.DataBind();
foreach(DataGridItem dgi in dgShow.Items)
{
//以下绑定非编辑状态下拉列表
DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
if(ddI!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddI.SelectedIndex = 0;
else
ddI.SelectedIndex = 1;
}
//以下绑定编辑状态下拉列表
DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
if(ddE!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddE.SelectedIndex = 0;
else
ddE.SelectedIndex = 1;
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();
}
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex = e.NewPageIndex;
BindData();
}
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
////////////////////////////////////////////////////////////
//说明:执行制定SQL语句/////////////////////////////////////
///////////////////////////////////////////////////////////
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(Exception e)
{
Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
}
}
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((DropDownList)(e.Item.FindControl("ddlSexE"))).SelectedItem.Value;
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();
}
}
}
#10
不行的话,
qq:329295182
qq:329295182