ReoGrid Forum

Fast and powerful .NET Spreadsheet Component

You are not logged in.

Announcement

This forum has been archived and no longer accepts new user registrations. Please report your questions, problems, and feedback to the issue page of ReoGrid on GitHub. Thank you for your cooperation.

https://github.com/unvell/ReoGrid/issues

#1 2014-06-09 05:07:49

VitekST
Member
Registered: 2014-06-09
Posts: 3

Embedding DB data in grid

Hello everyone!
I have some problems with embedding database data in the grid.

I have a DataTable object, which is created from a database connection (in this case, SQLite, but that has no matter)

And I want to embed all the contents of DataTable object to the grid.

I'm sharing the example, what I want to achieve.

Here is an example:
6ccb5ca9391af3dc084cb46af74d0834.png

I tried everything what I had on my mind, but nothing worked.

Please, help me!

Thanks for all answers,
VitekST

Last edited by VitekST (2014-06-09 05:08:45)

Offline

#2 2014-06-09 08:12:46

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: Embedding DB data in grid

Try this code:

// get a DataTable from database
var adapter = new DataSet1TableAdapters.MyTableAdapter();
var dt = adapter.GetData();

// iterate to copy all cells
for (int r = 0; r < dt.Rows.Count; r++)
{
	for (int c = 0; c < dt.Columns.Count; c++)
	{
		grid[r, c] = dt.Rows[r][c];
	}
}

P.S. ReoGrid cannot read a DataTable now, it is necessary to copy the field one by one.

Last edited by Jingwood (2014-06-09 08:13:21)

Offline

#3 2014-06-09 13:16:54

VitekST
Member
Registered: 2014-06-09
Posts: 3

Re: Embedding DB data in grid

Thank you very much, it helped a lot!

Here is improved code:

            var data = new DataTable();

            adapter.Fill(data);

            var data_range = new ReoGridRange(refEdit1.Text);

            var title_range = new ReoGridRange(data_range.StartPos, new ReoGridPos(data_range.StartPos.Row, data_range.StartPos.Col + data.Columns.Count - 1));


            List<object> captions = new List<object>();

            foreach (DataColumn c in data.Columns) captions.Add(c.Caption);


            Control.DoAction(new RGSetRangeStyleAction(title_range, new ReoGridRangeStyle()
            {
                Flag = PlainStyleFlag.FontStyleBold,
                Bold = true
            }));

            Control.DoAction(new RGSetRangeStyleAction(title_range, new ReoGridRangeStyle()
            {
                Flag = PlainStyleFlag.FontStyleUnderline,
                Underline = true
            }));


            Control.DoAction(new RGSetRangeBorderAction(title_range, ReoGridBorderPos.All,
                new ReoGridBorderStyle { Color = Color.Black, Style = BorderLineStyle.Solid }));


            for (int c = 0; c < title_range.Cols; c++)
            {
                Control[title_range.Row, title_range.Col + c] = data.Columns[c].Caption; 
            }

            for (int r = 0; r < data.Rows.Count; r++)
            {
                for (int c = 0; c < data.Columns.Count; c++)
                {
                    Control[data_range.Rows + r + 1, data_range.Cols + c] = data.Rows[r][c].ToString();
                }
            }

            var all_range = new ReoGridRange(title_range.StartPos, new ReoGridPos(data.Rows.Count + 1, data.Columns.Count));

            Control.DoAction(new RGSetRangeBorderAction(all_range, ReoGridBorderPos.Outline,
                new ReoGridBorderStyle { Color = Color.Black, Style = BorderLineStyle.BoldSolid }));

VitekST

Offline

#4 2014-06-09 13:29:45

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: Embedding DB data in grid

Nicely done!

Why you need to set the styles and borders by using Actions? Another way is to call API of the control directly rather than DoAction, unless that you need to undo the operations.

DoAction(new RGSetRangeBorderAction) => Control.SetRangeBorder()
DoAction(new RGSetRangeStyleAction) => Control.SetRangeStyle()
...

Offline

#5 2014-06-10 14:52:39

VitekST
Member
Registered: 2014-06-09
Posts: 3

Re: Embedding DB data in grid

Thanks for tip!
I didn't notice that...

Offline

#6 2015-07-24 06:26:52

rahul198
Member
Registered: 2015-07-17
Posts: 1

Re: Embedding DB data in grid

Dear Jing,

Thanks for the tool!

Can it is possible to export and import the data from Grid to a database(Access or Sql Server).
Please tell me the possible methods.

Offline

#7 2015-07-24 12:35:40

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: Embedding DB data in grid

Hi rahul198,

Welcome! Currently ReoGrid cannot directly access database. You might should to get data from database, then set them into grid by programming.

Jing

Offline

Board footer

Powered by FluxBB