简体   繁体   中英

Only top row of DataGridView updating?

I have a DataGridView that I'm populating from a list. The function that edits this list is called 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.

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:

//Moved above in EDIT 3

EDIT 3 : Posted whole 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. That should do.

Update

There's a ResetBindings() in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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