繁体   English   中英

仅更新DataGridView的第一行?

[英]Only top row of DataGridView updating?

我有一个要从列表中填充的DataGridView。 编辑此列表的函数称为LoadCollectionData()' 多余的行可以很好地添加到列表中,并且添加该行时将填充与该行相关的相关数据。

问题在于,以后在更改其他数据而更改了数据网格上显示的内容时,只有最上面的行继续更新,所有其他行保持不变。

这是该方法的代码:

    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);
    }

更奇怪的是,当我单击任何行时,它们都立即更新 但是,除非单击该行(除非它是第一行),否则它不会更新。

因此,我怀疑可能需要在dataGrid本身上设置一些属性,或者我需要以某种方式告诉每一行通过代码刷新。

有什么问题?

编辑 :看来,单击更新的单元格的唯一原因是因为我主动从单元格中获取内容。 我注释掉了下面的代码,即使单击它也停止更新。 然后,它只会更新第一行的值,仅此而已。

码:

//Moved above in EDIT 3

编辑2:这是KeywordDensity的类声明:

//Moved above in EDIT 3

编辑3 :张贴整个schebang。

我稍微修改了代码,请尝试使用此代码。

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

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

调用datagrid的DataBind()方法。 那应该做。

更新资料

在这种情况下有一个ResetBindings()

暂无
暂无

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

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