Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否仅更新DataGridView的顶行?_C#_Windows_Winforms_Visual Studio 2010_Datagridview - Fatal编程技术网

C# 是否仅更新DataGridView的顶行?

C# 是否仅更新DataGridView的顶行?,c#,windows,winforms,visual-studio-2010,datagridview,C#,Windows,Winforms,Visual Studio 2010,Datagridview,我有一个从列表中填充的DataGridView。编辑此列表的函数称为LoadCollectionData()'。额外的行可以很好地添加到列表中,当添加行时,与该行相关的数据就会填充 问题是,稍后当其他数据被更改时,会改变datagrid上显示的内容,只有最上面的一行继续更新,其他所有数据保持不变 下面是该方法的代码: public bool haschanged = false; public class KeywordDensity { public

我有一个从列表中填充的DataGridView。编辑此列表的函数称为
LoadCollectionData()'
。额外的行可以很好地添加到列表中,当添加行时,与该行相关的数据就会填充

问题是,稍后当其他数据被更改时,会改变datagrid上显示的内容,只有最上面的一行继续更新,其他所有数据保持不变

下面是该方法的代码:

    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);
    }
编辑2:以下是关键字密度的类声明:

//Moved above in EDIT 3

编辑3:发布整个schebang。

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

调用datagrid的方法。这样就可以了

更新


在这种情况下有一个

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

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

我试过了。不幸的是,这对我不起作用。我调用了以下函数进行尝试:dataGrid.DataSource=LoadCollectionData();dataGrid.Invalidate();该死dataGrid.Refresh();也不行。没有数据绑定方法。我使用的是DataGridView,而不是GridView。不幸的是,我似乎也没有这种方法。如果有什么不同的话,我用的是VS2010。这可能是新行未“提交”或其他问题吗?我不知道这是否可能,但我只是觉得非常奇怪,在我点击每一个之后,它们都开始更新,我上面包含的代码从点击的单元格中获取值。这个问题令人困惑。感谢insta的回复!尝试在网格的click事件上设置断点a查看后面发生了什么…或显示调用堆栈所有调用堆栈显示的是,当我在从单元格值复制字符串的代码部分上设置断点时,出现了MouseUp事件,或者在事件本身上..如何将数据源分配给gridYes。当在我的主文本框中更改文本时,我调用:dataGrid.Datasource=LoadCollectionData();好的,看来我们现在需要查看整个代码。。你能展示一下吗…好的,贴在整个项目附近。如果有办法发布DataGridView的属性,我也会这样做,尽管我已经删除了当前的一个,并尝试了一个全新的代码,该代码没有以任何方式从默认更改,它仍然做同样的事情。谢谢谢克哈鲁专业!有意思。。在这篇文章中,我在MSDN论坛上发现它提到了一个问题,一行DataGridView被选中,干扰了他的更新。--我注意到,只要我开始键入DataGridView的顶行,它就会被选中,即使它没有焦点。
string[] keywordsarray = keywordslower.Split
    (new char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries);