只有DataGridView的顶行更新?

时间:2011-02-02 06:07:03

标签: c# windows winforms visual-studio-2010 datagridview

我有一个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。

3 个答案:

答案 0 :(得分:1)

我稍微修改了代码,试试这段代码。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

<强>更新

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