You are not logged in.
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.
Pages: 1
Topic closed
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.
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
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
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
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
Thanks for tip!
I didn't notice that...
Offline
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
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
Pages: 1
Topic closed