开发者

How to display data from a SQLite database into a GTK# TreeView?

OS: openSuse 11.4 IDE: MonoDevelop 2.4.2 using GTK#

I need to display the datatable of a sqlite database in a simple grid view, much like in WindowsForms'/WPF's datagridview, but with GTK#. I have been trying to configure the GTK.TreeView to display the data properly, but with no luck. The data is not displayed and I get an obscure error in the Application output. Here is my code:

            Type[] types;           
            SqliteCommand cmd = new SqliteCommand("SELECT * FROM "+Tables.USERS, _cddapConn);
            cmd.Connection.Open();
            SqliteDataReader reader = cmd.ExecuteReader();
            types = new Type[reader.FieldCount];        


        for(int i = 0; i < types.Length; i++)
            types[i] = typeof(string);
        Gtk.ListStore list = new Gtk.ListStore(types);

        for(int i = 0; i < TblUsers.SCHEMA.Length; i++)
        {               
            table.AppendColumn(TblUsers.SCHEMA[i], new Gtk.CellRendererText(), "text");
        }
while(reader.Read())
        {
            String[] rowData = new String[TblUsers.SCHEMA.Length];
            for(int index = 0; index < TblUsers.SCHEMA.Length; index++)
            {
                rowData[index] = reader.GetString(index);                   
                table.Columns[index].AddAttribute(new Gtk.CellRendererText(), "text", index);

                }
                list.AppendValues(rowData);


        }
        table.Model = list;
        reader.Close();
        cmd.Connection.Close();

I followed the example given here: http://www.mono-project.com/GtkSharp_TreeView_Tutorial. First I create the model (ListStore) by initializing it with the string type for the data of every column. Then I append the columns of the data table to the tree view. Then I engage the sqlite reader, and for every entry I add its data to the model. Then I add a cell for each column pointing to the data. Finally, I give the tree view its model.

However, this only manages to display the columns with no data. What I get in the application output is this:

Gtk-CRITICAL **: gtk_tree_view_column_cell_layout_add_attribute: assertion `info != NULL' failed

I spent a few hours looking for any information about this problem with no success. What is surprising is that I found no examples of the treeview being used in mono to display data fr开发者_JS百科om a database, or any documentation on gtk# for that matter.

How can I make my grid view work? I really only need it to display the data and accept row selections (so that I can check the ID column of the selected row).


I saw the very same as you, Following the GtkSharp TreeView Tutorial gives the exact same assertion and no data in the tree. I think the Assert is a red herring. In any case, I got what you were aiming for with the following code.

  ListStore SetupModel( TreeView tv ){
    var m = new ListStore(typeof(string),typeof(string));

    var nameCol = new TreeViewColumn( "Name", 
      new CellRendererText(), "text", 0 );
    tv.AppendColumn( nameCol );

    var colourCol = new TreeViewColumn( "Colour", 
      new CellRendererText(), "text", 1 );
    tv.AppendColumn( colourCol );

    tv.Model = m;
    return m;
  }

  void PopulateData( ListStore model ) {
    model.AppendValues( "Fred", "Blue" );
    model.AppendValues( "Bob", "Green" );
    model.AppendValues( "Mary", "Yellow" );
    model.AppendValues( "Alice", "Red" );
  }


I stumbled upon this thread while looking for a way to display a datatable in GTK with a treeview.

Ended up writing a small function that takes the datatable returned by the select query and loading it to the treeview (dtResult, already defined with the Designer)

private void loadResults(ref DataTable table)
{
    //Remove previous model
    foreach (var col in dtResult.Columns)
    {
        dtResult.RemoveColumn(col);
    }

    //Set the number of columns to type string 
    //(could use Row.DataType, but would require some work when adding values)
    List<System.Type> colTypes = new List<System.Type>();
    for (int col_it = 0; col_it < table.Columns.Count; col_it++)
    {
        colTypes.Add(typeof(string));
    }
    ListStore resultListStore = new ListStore(colTypes.ToArray());

    //Adding columns
    for (int col_it = 0; col_it < table.Columns.Count; col_it++)
    {
        dtResult.AppendColumn(table.Columns[col_it].ColumnName, new CellRendererText(), "text", col_it);
    }

    //Adding values
    List<string> rowValues = new List<string>();
    for (int row_it = 0; row_it < table.Rows.Count; row_it++)
    {
        for (int col_it = 0; col_it < table.Columns.Count; col_it++)
        {
            rowValues.Add(table.Rows[row_it][col_it].ToString());
        }
        resultListStore.AppendValues(rowValues.ToArray());
        rowValues.Clear();
    }

    //updating model
    dtResult.Model = resultListStore;
}

This is just a quick way to explore a dataset, mainly for debugging. Proceed with caution for any production use

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜