很简单,就是我的思路被卡住了
数据库里有张8列N行的表
在一个窗体上进行删除的功能,
请问用什么控件
数据库里有张8列N行的表
在一个窗体上进行删除的功能,
请问用什么控件
解决方案 »
- 求一段js删除cookie代码
- C# 事件注册过程中,是否会导致程序死掉
- 关于C#类问题
- C#TreeView对指定节点增加上游或下游,怎么实现呢?内详。提供实现源码都有分。
- 这样写没有输出,错在哪里??
- 求救!一个listview有单击和双击事件,请问如何在双击时避免响应单击事件?
- 请各位帮忙给看看,怎么解决,关于DataGrid数据邦定的问题
- 高手进来看一下正则表达式吧!
- 关于生成.exe文件
- 我知道了接口及其实现方法,但怎么用它呢,我不能为接口创建一个实例,该用什么方法访问他呢?
- 各位看官请过来,救救我这个莫名其妙的对话框关闭的问题
- 索引器的问题(编了两年程,突然对一基本问题没办法了,请基础扎实的同仁指教)
.aspx文件:
<asp:datagrid id=dgdBranch runat="server" Width="445px" DataSource="<%# BraTB %>" DataKeyField="branchid" BorderWidth="1px" BackColor="#F7F7F7" BorderColor="#F7F7F7" CssClass="table" BorderStyle="Solid" AutoGenerateColumns="False" Font-Size="X-Small">
<SelectedItemStyle BackColor="Transparent"></SelectedItemStyle>
<EditItemStyle ForeColor="Black" BackColor="Black"></EditItemStyle>
<AlternatingItemStyle BorderColor="White" BackColor="White"></AlternatingItemStyle>
<ItemStyle CssClass="textcenter" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle CssClass="headcenter"></HeaderStyle>
<FooterStyle CssClass="headcenter"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="BranchName" HeaderText="机构名称"></asp:BoundColumn>
<asp:BoundColumn DataField="SimpleCode" HeaderText="机构简称"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="更新机构信息">
<ItemTemplate>
<asp:LinkButton runat="server" Text="编辑" CommandName="Edit" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="更新" CommandName="Update" CausesValidation="false"></asp:LinkButton>
<asp:LinkButton runat="server" Text="取消" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除" ItemStyle-Width="10%">
<ItemTemplate>
<asp:ImageButton CausesValidation="False" id="Imagebutton2" runat="server" ImageUrl="../img/delete.gif" CommandName="delete" AlternateText="删除此项机构"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
.aspx.cs文件:
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 qminoa.BLL;
using qminoa.Common;
using qminoa.Common.Data;namespace qminoa.Webs.MR
{
public class BranchSet : qminoa.Webs.PageBase
{
protected System.Web.UI.WebControls.Label lblBranchID;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.Label lblErr;
protected System.Web.UI.WebControls.DataGrid dgdBranch;
protected System.Web.UI.WebControls.LinkButton cmdAdd;
protected System.Web.UI.WebControls.RequiredFieldValidator valSimCode;
protected System.Web.UI.WebControls.TextBox txtSimCode;
protected System.Web.UI.WebControls.RequiredFieldValidator valName;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
private void Page_Load(object sender, System.EventArgs e)
{
this.PageBegin("机构管理",true);
if(!Page.IsPostBack)
{
DataBind();
}
} public DataTable BraTB
{
get
{
return (new DepSystem()).GetBraTB();
}
} #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.cmdAdd.Click += new System.EventHandler(this.cmdAdd_Click);
this.dgdBranch.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdBranch_CancelCommand);
this.dgdBranch.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdBranch_EditCommand);
this.dgdBranch.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdBranch_UpdateCommand);
this.dgdBranch.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdBranch_DeleteCommand);
this.dgdBranch.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgdBranch_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void dgdBranch_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(this.EmpRightCode >= 3)
{
int index = e.Item.ItemIndex;
dgdBranch.EditItemIndex = index;
dgdBranch.DataBind();
}
else
JScript.Alert("您没有权限进行此操作!");
} private void dgdBranch_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(this.EmpRightCode == 4)
{
lblErr.Visible = false;
int index = e.Item.ItemIndex;
int braID = Convert.ToInt16(dgdBranch.DataKeys[index].ToString(),10);
try
{
bool result = (new DepSystem()).DeleteMrBranch(braID);
this.WriteOptLog("删除机构"+braID.ToString()+"信息");
}
catch
{
lblErr.Visible = true;
lblErr.Text = "存在和该机构相关联的信息,不能删除!";
}
dgdBranch.DataBind();
}
else
JScript.Alert("您没有权限进行此操作!");
} private void dgdBranch_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int index = e.Item.ItemIndex;
dgdBranch.EditItemIndex = -1;
dgdBranch.DataBind();
} private void dgdBranch_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(this.EmpRightCode >= 3)
{
int index = e.Item.ItemIndex;
int braID = Convert.ToInt16(dgdBranch.DataKeys[index].ToString(),10); string s1 = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
string s2 = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
if(!ValidateUtil.isBlank(s1))
{
bool result = (new DepSystem()).UpdateMrBranch(braID,s1,s2);
this.WriteOptLog("修改机构"+s1+"信息");
dgdBranch.EditItemIndex = -1;
dgdBranch.DataBind();
}
else
JScript.Alert ("机构名称不能为空!");
}
else
JScript.Alert("您没有权限进行此操作!");
} private void cmdAdd_Click(object sender, System.EventArgs e)
{
if(this.EmpRightCode >= 2)
{
bool result = (new DepSystem()).InsertMrBranch(txtName.Text,txtSimCode.Text);
this.WriteOptLog("添加机构"+txtName.Text.ToString()+"信息");
dgdBranch.DataBind();
}
else
JScript.Alert("您没有权限进行此操作!");
} private void dgdBranch_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';" ;
if (itemType == ListItemType.Item )
{
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#dedfde';";
}
else if( itemType == ListItemType.AlternatingItem)
{
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
}
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
ImageButton button = (ImageButton) e.Item.FindControl("Imagebutton2");
button.Attributes.Add ("onclick",
"return confirm (\"确定要删除此项记录吗?\");");
}
}
}
}
可以不可以给些参考代码啊,谢谢啊
{
DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(e.X, e.Y);
try
{
if( hti.Type == DataGrid.HitTestType.Cell &&
hti.Column == 2)
{
this.dataGrid1[hti.Row, hti.Column] = ! (bool) this.dataGrid1[hti.Row, hti.Column];
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
DataTable dt = (DataTable)dataGrid1.DataSource;
bool b;
for(int i =0;i<dt.Rows.Count;i++)
{
if((bool)dataGrid1[i,2]== true)
{
//执行操作 }
}
DataTable dt = (DataTable)dataGrid1.DataSource;
bool b;
for(int i =0;i<dt.Rows.Count;i++)
{
if((bool)dataGrid1[i,2]== true)
{
//执行操作 }
}
这些代码是不是放在form_load里的还是你少写了finally啊?
if (bm.Count <= 0 || bm.Position == -1)
return;
DataRow dr = ((DataRowView)bm.Current).Row;
dr.Delete();
这段代码在DataGrid控件里记录是删除了,可是没有删除数据库里的,高手请帮帮忙
调用sqlDataAdapter.update()
或者SqlCommand.ExecuteNonQuery()
至于sqlDataAdapter或者SqlCommand
如何创建,可以从数据库资源管理器拖一个表放到FORM上,
系统会自动创建相关代码。你看看就知道了.
delete * form 表名 where 列名='"++"'
where后面怎么写我怎么取到我要删除的那一行,谢谢
关键是把sqlDataAdapter或者SqlCommand创建好,
才能调用相应的方法。其他的事update方法会做好的。如果不用系统自动创建的sqlDataAdapter代码,
可以用sqlCommandBuilder创建,自己看看相关帮助 ,
才能更好地获取知识。
insertCommand和updateCommand、deleteCommand,然后调用
update()方法即可可参考sqlConnection1 = new System.Data.SqlClient.SqlConnection();
sqlConnection1.ConnectionString = "workstation id=MyComputer;user id=sa;data source=MyComputer;persist security info=False;initial catalog=MyDataBase";SqlCommand sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
sqlSelectCommand1.CommandText = "SELECT ID, Name FROM Users";
sqlSelectCommand1.Connection = sqlConnection1;DataSet ds = new DataSet();
SqlDataAdapter sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
SqlDataAdapter.SelectComman = sqlSelectCommand1;sqlDataAdapter1.Fill(ds);
/*
这里插入修改DataSet数据的代码
*/ sqlConnection1.Connected = true;
SqlCommandBuilder a = new SqlCommandBuilder(sqlDataAdapter1);//提交到数据库
sqlDataAdapter1.Update(ds);