当前位置:编程学习 > C#/ASP.NET >>

请教C# datagridview 添加数据后刷新问题

环境:
1. 一个bindingNavigator1;一个bindingSource1;七个TextEdit;一个DatagridView
采用三层架构代码如下:
DAL:
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select xsicid,xsic01,xsic02,xsic03,xsic04,xsic05,xsic06,xsic07 ");
            strSql.Append(" FROM tb_test ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
******************
xsicBLL:
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
*******************
界面层:
        DataSet InitDS = new DataSet();
        private void InitData() //初始化
        {
            //
            InitDS = xsicBll.GetList("xsicid>0 Order by xsicid");
        }
        private void BindingControl() //绑定数据到
        {
            this.bindingSource1.DataSource = InitDS;
            this.bindingSource1.DataMember = InitDS.Tables[0].TableName;
            this.bindingNavigator1.BindingSource = this.bindingSource1;

            //Datagridview.DataSource 设置为bindingSource1.DataSource  
            this.txtxsic01.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic01", true));
            this.txtxsic02.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic02", true));
            this.txtxsic03.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic03", true));
            this.txtxsic04.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic04", true));
            this.txtxsic05.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic05", true));
            this.txtxsic06.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic06", true));
            this.txtxsic07.DataBindings.Add(new Binding("EditValue", this.bindingSource1, "xsic07", true));
        }//初始化显示数据
        private void frm_Trxs_Load(object sender, EventArgs e)
        {
            //
            this.BindingControl();
        }
//
以上代码,在我打开窗口时数据都能正常显示,现在的问题是:
1.在一个界面,我点击"添加"按钮,在7个TextEdit中录入新的数据后,数据能添加到数据库,但是在datagridview中没有显示出来,也就是没有刷新数据。请教这个刷新方法要如何写:
        private void RefreshAllItem() \\刷新
        {
           //
        }

添加代码如下:
        private void toolsave_Click(object sender, EventArgs e)
        {
            //
            xsic.xsic01 = txtxsic01.Text;
            xsic.xsic02 = txtxsic02.Text;
            xsic.xsic03 = txtxsic03.Text;
            xsic.xsic04 = decimal.Parse(txtxsic04.Text);
            xsic.xsic05 = txtxsic05.Text;
            xsic.xsic06 = txtxsic06.Text;
            xsic.xsic07 = txtxsic07.Text;
            xsic.xsic08 = Session.UserAllName;
            xsic.xsic09 = DateTime.Now.ToShortDateString().ToString();//
            xsic.xsic10 = txtxsic07.Text;
            //
                    try
                    {
                        xsicBll.Add(xsic); //添加没有问题
                        //
                        MessageBox.Show("添加成功!是否继续添加?", "操作提示");
                        this.RefreshAllItem();//如何写?
                        //如果我调动BindingControl(),会提示"这将导致集合中的两个绑定到同一个属性,参数名 binding"
                    }
                    catch (System.Exception ex)
                       {
                        MessageBox.Show(ex.Message,"System Error");
                       }
                   } Datagridview C C# Binding dataset --------------------编程问答-------------------- 在添加按钮后成功判断里加入重新绑定数据。
也就是调用你绑定datagridview的方法。 --------------------编程问答-------------------- 你的分层代码有点怪

try
                     {
                         xsicBll.Add(xsic); //添加没有问题
                        //
xsicBLL bll = new xsicBLL();
bll.BindingControl();
                         MessageBox.Show("添加成功!是否继续添加?", "操作提示");
                         this.RefreshAllItem();//如何写?
                        //如果我调动BindingControl(),会提示"这将导致集合中的两个绑定到同一个属性,参数名 binding"
                     } --------------------编程问答-------------------- 你应该将
private void BindingControl() //绑定数据到
写到CS代码中,而不是写到 BLL里 --------------------编程问答-------------------- 重新获取数据
然后重置datagridview数据源 --------------------编程问答-------------------- 回二楼:
namespace TrimAppSystem.TrimXS
{
    public partial class frm_Trxs : Form
    {
        CtbXsIc xsic = new CtbXsIc();  //前面有定义好的
        CtbXsIcBLL xsicBll = new CtbXsIcBLL(); //前面有定义好的 --------------------编程问答-------------------- 回三楼:
private void BindingControl()我是写在CS中的。界面层 --------------------编程问答-------------------- 重新加载 绑定数据方法啦lz --------------------编程问答-------------------- 重新加载数据 --------------------编程问答-------------------- 在xsicBll.Add(xsic); 前面添加
this.bindingSource1.EndEdit();
再写个  private void RefreshAllItem() \\刷新
        {
           //
            DataSet InitDS = new DataSet();

            this.bindingSource1.DataSource = ReDS;
            this.bindingSource1.DataMember = ReDS.Tables[0].TableName;
            this.bindingNavigator1.BindingSource = this.bindingSource1;
            //
            录入的所有TextEdit不再绑定,初始化的时候绑定
        }
以上,好像是可以添加并刷新出数据来了。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,