简体   繁体   English

仅更新DataGridView的第一行?

[英]Only top row of DataGridView updating?

I have a DataGridView that I'm populating from a list. 我有一个要从列表中填充的DataGridView。 The function that edits this list is called LoadCollectionData()' . 编辑此列表的函数称为LoadCollectionData()' Extra rows get added to the list just fine, and the relevant data pertaining to that row populates when the row is added. 多余的行可以很好地添加到列表中,并且添加该行时将填充与该行相关的相关数据。

The problem is that later on when other data is being changed that'd alter what's displayed on the datagrid, only the top row continues to update, all of the others remain the same. 问题在于,以后在更改其他数据而更改了数据网格上显示的内容时,只有最上面的行继续更新,所有其他行保持不变。

Here's the code for the method: 这是该方法的代码:

    public bool haschanged = false;

    public class KeywordDensity
    {
        public bool included { get; set; }
        public string keyword { get; set; }
        public string occurences { get; set; }
        public string density { get; set; }
    }

    public int WordCount(string txtToCount)
    {
        string pattern = "\\w+";
        Regex regex = new Regex(pattern);

        int CountedWords = regex.Matches(txtToCount).Count;

        return CountedWords;
    }

    public int KeywordCount(string txtToCount, string pattern)
    {
        Regex regex = new Regex(pattern);

        int CountedWords = regex.Matches(txtToCount).Count;

        return CountedWords;
    }


    public List<KeywordDensity> LoadCollectionData()
    {
        string thearticle = txtArticle.Text.ToLower();
        string keywordslower = txtKeywords.Text.ToLower();
        string[] keywordsarray = keywordslower.Split('\r');
        List<KeywordDensity> lsikeywords = new List<KeywordDensity>();
        bool isincluded = false;
        double keywordcount = 0;
        double wordcount = WordCount(thearticle);
        double thedensity = 0;



        foreach (string s in keywordsarray)
        {

            if (s != "")
            {
                keywordcount = KeywordCount(thearticle, s);
                thedensity = keywordcount / wordcount;
                thedensity = Math.Round(thedensity, 4) * 100;


                if (thearticle.Contains(s))
                {
                    isincluded = true;
                }
                else
                {
                    isincluded = false;
                }


                lsikeywords.Add(new KeywordDensity()
                {
                    included = isincluded,
                    keyword = s,
                    occurences = keywordcount.ToString(),
                    density = thedensity.ToString() + "%"
                });

            }

        }

        return lsikeywords;
    }

    private void txtArticle_TextChanged(object sender, EventArgs e)
    {
        if (haschanged == false)
            haschanged = true;

        lblWordCountNum.Text = WordCount(txtArticle.Text).ToString();

        dataGrid.DataSource = LoadCollectionData();

    }

    private void dataGrid_MouseUp(object sender, MouseEventArgs e)
    {
        int cursorpos = 0;
        string copied = "";

        if (dataGrid.CurrentCellAddress.X == 1) //Only grab content if the "Keyword" column has been clicked on
            copied = " " + dataGrid.CurrentCell.Value.ToString() + " ";

        cursorpos = txtArticle.SelectionStart;
        txtArticle.Text = txtArticle.Text.Insert(cursorpos, copied);
    }

What's even more odd, is that when I click on any of the rows, then they immediately update . 更奇怪的是,当我单击任何行时,它们都立即更新 However, unless the row is clicked on (unless it's the top one) it doesn't update. 但是,除非单击该行(除非它是第一行),否则它不会更新。

Because of this, I suspect there may be some property I need to set on the dataGrid itself, or I need to somehow tell each row to refresh through code. 因此,我怀疑可能需要在dataGrid本身上设置一些属性,或者我需要以某种方式告诉每一行通过代码刷新。

What's the dealio? 有什么问题?

EDIT : It appears that the only reason that the cell that's clicked on updates is because I actively grab content from the cell. 编辑 :看来,单击更新的单元格的唯一原因是因为我主动从单元格中获取内容。 I commented out the code below and it stopped updating even when clicked on. 我注释掉了下面的代码,即使单击它也停止更新。 It then would only update the top row's values and that's it. 然后,它只会更新第一行的值,仅此而已。

Code: 码:

//Moved above in EDIT 3

EDIT 2: Here's the class declaration for KeywordDensity: 编辑2:这是KeywordDensity的类声明:

//Moved above in EDIT 3

EDIT 3 : Posted whole schebang. 编辑3 :张贴整个schebang。

I modified the code slightly, try this code. 我稍微修改了代码,请尝试使用此代码。

string[] keywordsarray = keywordslower.Split
    (new char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries);

您可能需要使Invalidate()控件触发重新绘制。

call the DataBind() method of the datagrid. 调用datagrid的DataBind()方法。 That should do. 那应该做。

Update 更新资料

There's a ResetBindings() in that case. 在这种情况下有一个ResetBindings()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM