很简单,就是我的思路被卡住了
数据库里有张8列N行的表
在一个窗体上进行删除的功能,
请问用什么控件

解决方案 »

  1.   

    示例代码:
    .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>&nbsp;
    <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 (\"确定要删除此项记录吗?\");");
    }
    }
    }
    }
      

  2.   

    那用DataGrid的技术含量有点高,我不知道怎么获取DataGrid里的值和数据库里的值匹配,还有DataGrid必须是动态的,按了删除按扭在DataGrid里也就删除,是不是很麻烦啊
      

  3.   

    我是在WinForm里,不是在ASP.NET!!大哥哥门看清楚。
      

  4.   

    只用一个复选框选中你要删除的数据,然后执行Sql即可.
      

  5.   

    to;renyu732(任宇@http://renyu732.cnblogs.com/) 
    可以不可以给些参考代码啊,谢谢啊
      

  6.   

    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    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)
    {
    //执行操作 }
    }
      

  7.   

    只要能进行数据绑定的控件都可以。控件会绑定到dataset的datatable,而在控件绑定数据,选择具体数据行,都可以使用BindingManagerBase.Current得到当前对象。再转换成DataRow,并调用其Delete方法即可。
      

  8.   

    具体代码你看MSDN中BindingManagerBase.Current中的举例吧
      

  9.   

    to: bitsbird(一瓢 在路上...) 
    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啊?
      

  10.   

    BindingManagerBase bm = dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
    if (bm.Count <= 0 || bm.Position == -1) 
    return;
    DataRow dr = ((DataRowView)bm.Current).Row;
    dr.Delete();
    这段代码在DataGrid控件里记录是删除了,可是没有删除数据库里的,高手请帮帮忙
      

  11.   

    你在Grid里只是修改了DATASET本地缓存的值,并没有提交到数据库。可以sqlDataAdapter或者SqlCommand提交
    调用sqlDataAdapter.update()
    或者SqlCommand.ExecuteNonQuery()
    至于sqlDataAdapter或者SqlCommand
    如何创建,可以从数据库资源管理器拖一个表放到FORM上,
    系统会自动创建相关代码。你看看就知道了.
      

  12.   

    关键在delete语句不知道怎么写
    delete * form 表名 where 列名='"++"'
    where后面怎么写我怎么取到我要删除的那一行,谢谢
      

  13.   

    这些语句都是参数化的,不用具体指定值。
    关键是把sqlDataAdapter或者SqlCommand创建好,
    才能调用相应的方法。其他的事update方法会做好的。如果不用系统自动创建的sqlDataAdapter代码,
    可以用sqlCommandBuilder创建,自己看看相关帮助 ,
    才能更好地获取知识。
      

  14.   

    只要创建好sqlDataAdapter.selectCommand,用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);