asp.net中gridview的绑定数据源实现增删改查

gridview还是比较灵活的,有一种类似于excel的用法,可以直接在表格中进行添加,修改等操作。单元格数据比较段的表格用起来很方便。

可以参考https://blog.csdn.net/yingzhaom/article/details/50614599

我在这基础上改成了点击按钮弹出模态框进行操作。如有此类需求可以参考我的代码。

先从绑定数据源开始。

页面加载时绑定数据(可以把数据库连接封装,后面会多次使用):

//数据库连接
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                //初始化table
                bind_gv();
            }
        }
protected void bind_gv()
        {
            string sqlstr = "select * from 表";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "表");
            gvTag.DataSource = myds;
            gvTag.DataKeyNames = new string[] { "tid" };
            gvTag.DataBind();
            sqlcon.Close();
        }

前端代码(表中列的dataField与查询的列名对应):


                    
                        
                        
                        
                        
                        
                        
                        
                        
                           
                               
                               
                               
                           
                           
                       
                    
                    
                    
                        
页数:

分页后台代码:

protected void gvTag_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            gvTag.PageIndex = e.NewPageIndex;
            bind_gv();
        }
        
        protected void gvTag_DataBound(object sender, EventArgs e)
        {
            // 取得显示页数的那一列。
            GridViewRow pagerRow = gvTag.BottomPagerRow;
            if (pagerRow != null)
            {
                // 取得显示目前所在页数的 Label 控件。
                Label pageLabel = (Label)(pagerRow.Cells[0].FindControl("lblCurrentPage"));

                // 取得 第一页、上一页、下一页、最后页 的按钮。
                ImageButton imgBtnFirst =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnFirst"));
                ImageButton imgBtnPrev =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnPrev"));
                ImageButton imgBtnNext =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnNext"));
                ImageButton imgBtnLast =
                  (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnLast"));

                // 设定何时应该显示 第一页、上一页、下一页、最后页 的按钮。
                if (gvTag.PageIndex == 0)
                {
                    imgBtnFirst.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnPrev.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                }
                else if (gvTag.PageIndex == gvTag.PageCount - 1)
                {
                    imgBtnLast.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnNext.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                }
                else if (gvTag.PageCount <= 0)
                {
                    imgBtnFirst.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnPrev.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnNext.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    imgBtnLast.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                }

                if (pageLabel != null)
                {
                    // 计算目前所在的页数。
                    int currentPage = gvTag.PageIndex + 1;

                    pageLabel.Text = currentPage.ToString() +
                        " / " + gvTag.PageCount.ToString();
                }
            }
        }

前台弹框代码:


                

后台弹框和编辑代码(里面的dao和impl是封装的数据库连接和更新的代码,TagInfo是封装的实体类):

//编辑弹框弹出
        protected void Modify_Click(object sender, EventArgs e)
        {
            LinkButton link = sender as LinkButton;
            int index = Convert.ToInt32(link.CommandArgument);
            TagInfoDao tag = new TagInfoDaoImpl();
            TagInfo tagInfo = tag.findByTid(index);
            //赋值
            TidEdit.Text = index.ToString();
            TagEdit.Text = tagInfo.groupCode +","+ tagInfo.elementCode;
            TagNameEdit.Text = tagInfo.tagName;
            NotesEdit.Text = tagInfo.notes;
            //弹出模态框
            ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
            scriptManager.RegisterStartupScript(typeof(string), "", "$('#editModal').modal('show');", true);
            //查询
            VRInfoDao vr = new VRInfoDaoImpl();
            List vrList = vr.findVRInfo("");
            ddlVREdit.DataSource = vrList;
            ddlVREdit.DataTextField = "code";
            ddlVREdit.DataValueField = "VID";
            ddlVREdit.DataBind();
            ddlVREdit.SelectedIndex = tagInfo.vid - 1;
        }
        //编辑保存
        protected void SaveEdit_Click(object sender, EventArgs e)
        {
            TagInfo tag = new TagInfo();
            tag.tid = Convert.ToInt32(TidEdit.Text);
            tag.groupCode = TagEdit.Text.Split(',')[0];
            tag.elementCode = TagEdit.Text.Split(',')[1];
            tag.tagName = TagNameEdit.Text;
            tag.notes = NotesEdit.Text;
            tag.vid = Convert.ToInt32(ddlVREdit.SelectedValue);
            TagInfoDao tagDao = new TagInfoDaoImpl();
            if (tagDao.updata(tag))
            {
                Alert("修改成功!");
            }
            else
            {
                Alert("修改失败!");
            }
            bind_gv();
        }

Alert弹框代码:

public void Alert(string str_Message)
        {
            ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
            scriptManager.RegisterStartupScript(typeof(string), "", "alert('" + str_Message + "');", true);
        }

删除后台代码:

//删除
        protected void Delete_Click(object sender, EventArgs e)
        {
            


            LinkButton link = sender as LinkButton;
            int index = Convert.ToInt32(link.CommandArgument);
            TagInfoDao tag = new TagInfoDaoImpl();
            if (tag.delete(index))
            {
                //删除成功
                Alert("删除成功");
            }
            else
            {
                //删除失败
                Alert("删除失败");
            }
            bind_gv();
        }

新增跟编辑一样就不放代码了。

你可能感兴趣的:(asp.net,c#)