在保留索引的同时隐藏DataGridView行

时间:2015-11-16 16:42:16

标签: c# winforms datagridview

我有一个DataGridView用于显示自定义类的数据foo。

public class foo
{
    public string header{get;set;}
    public int value{get;set;}
    public bool isHidden{get;set;}
}

从数据库中读取Foo并从JSON字符串处理,然后插入到DataGridView中以显示给用户。

List<foo> listOfFoo = new List<foo>();
DataTable fooTable = new DataTable();
myDataGridView.DataSource = fooTable;

private void getFoo()
{
    JavaScriptSerializer js = new JavaScriptSerializer();
    string getData = myWebClient.DownloadString(/*url of server*/);

    Dictionary<string,object> fooArg = (Dictionary<string,object>)js.DeserializeObject(getData);
    object[] foo = (object[])fooArg["foo"];
    for(int i = 0; i < foo.Length; i++)
    {
        Dictionary<string,object> fooStep = (Dictionary<string,object>)foo[i];
        foo temp = new foo();

        temp.header = (string)fooStep["header"];
        temp.value = (int)fooStep["value"];
        temp.isHidden = (bool)fooStep["isHidden"];
        listOfFoo.Add(temp);
    }
    drawFoo();
}
private void drawFoo()
{
    fooTable.Rows.Clear();
    for(int i = 0; i < listOfFoo.Count; i++)
    {
        //myDataGridView has two columns, header and value. isHidden is not shown in the table itself
        foo i = listOfFoo.ElementAt(i);
        fooTable.Rows.Add(i.header, i.value);
    }
}

我遇到的问题是我正在使用行的索引来保留foos的顺序(DataGridView不可排序)。如果我将drawFoo函数修改为如下,是否会保留索引?我有一个插入函数,它将新的foo插入到DataGridView中当前选中的列表下方的列表中。它会不会搞砸索引,考虑到我将listOfFoo发布回服务器时它必须按照适当的顺序。有没有更好的方法来处理这个?我愿意接受建议。谢谢,人比我聪明!

private void drawFoo()
{
    fooTable.Rows.Clear();
    for(int q = 0; q < listOfFoo.Count; q++)
    {
        //myDataGridView has two columns, header and value. isHidden is not shown in the table itself
        foo i = listOfFoo.ElementAt(q);
        fooTable.Rows.Add(i.header, i.value);
        if(i.isHidden)
            myDataGridView.Rows[q].Visible = false;
    }
}

0 个答案:

没有答案