如何从另一种形式的按钮更新DataGridView?

时间:2017-06-10 09:56:54

标签: c# methods datagridview get

我有一个包含DataGridView的表单,我有另一个表单将数据插入数据库,我想从最后一个表单更新DataGridview 附在你身上 我的代码是它有时有效,有时它不会更新 仅当DataGridView为空时才会出现此问题

此代码格式为FRM_RECEIPTS

        private static FRM_RECEIPTS frm;

        static void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }
        public static FRM_RECEIPTS getMainForm
        {
            get
            {
                if (frm == null)
                {
                    frm = new FRM_RECEIPTS();
                    frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);

                }
                return frm;
            }
        }

此代码位于FRM_RECEIPT

内的button1中
 FRM_RECEIPTS frm = new FRM_RECEIPTS();
 FRM_RECEIPTS.getMainForm.dgvRecipts.DataSource
 = receipt.SearchRecepits(frm.txtSearch.Text, frm.coBoxState.Text);

SearchRecepits方法

public DataTable SearchRecepits(string criterion , string state)
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DataTable Dt = new DataTable();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@criterion", SqlDbType.VarChar, 50);
            param[0].Value = criterion;

            param[1] = new SqlParameter("@state", SqlDbType.VarChar, 50);
            param[1].Value = state;

            Dt = DAL.selectData("SearchRecepits", param);
            DAL.Close();
            return Dt;
        }

1 个答案:

答案 0 :(得分:0)

首先应将datasource设置为null,然后分配一个数据源以使DataGridView保持一致。

在将数据源分配给DataGridView

之前,将此行添加到button1
var pathToPictures = [Pictures]()
func updateAllImagesOfCurrentUserInDatabase() {

    //refrences
    let ref = FIRDatabase.database().reference()
    let uid = FIRAuth.auth()?.currentUser?.uid

    //match the user ID value stored into posts with current userID and get all the posts of the user
    let update = ref.child("posts").queryOrdered(byChild: "userID").queryEqual(toValue: uid)
    update.observe(FIRDataEventType.value, with: { (snapshot) in
        self.pathToPictures.removeAll()

        if snapshot.value as? [String : AnyObject] != nil {
        let results = snapshot.value as! [String : AnyObject]

        for (_, value) in results {
            let pathToPostPicture = Pictures()

            if let pathToImage = value["pathToImage"] as? String , let postID = value["postID"] as? String {
                pathToPostPicture.postImageUrl = pathToImage
                pathToPostPicture.postID = postID
                self.pathToPictures.append(pathToPostPicture)
                print("Image and POST ID: \(pathToPostPicture.postImageUrl!)")
                print("Post ID is : \(postID)")

                if FIRAuth.auth()?.currentUser?.uid == uid {
                ref.child("Users_Details").child(uid!).child("profileImageUrl").observeSingleEvent(of: .value, with: { (userSnapshot) in
                    print(userSnapshot)

                    let userIMageUrl = userSnapshot.value as! String
                    pathToPostPicture.postImageUrl = userIMageUrl
                    self.pathToPictures.append(pathToPostPicture)
                    print("This is the image path:" + userIMageUrl + String(self.pathToPictures.count))

                    // Update the Database

                    let postIDCount = pathToPostPicture.postID!
                    let updatedUserData = ["posts/\(postIDCount)/pathToImage": pathToPostPicture.postImageUrl!]
                    print("This is THE DATA:" , updatedUserData)
                    ref.updateChildValues(updatedUserData as Any as! [AnyHashable : Any])

                })
                }
                print(self.pathToPictures.count)
            }
            }
        } else {
            print("snapshot is nill - add some data")
        }

        self.collectionView?.reloadData()
    })
}

所以button1看起来像

FRM_RECEIPTS.getMainForm.dgvRecipts.DataSource = null;