We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How do I create a DataSet with the data entered in the Grid?

 

You can create a new dataset with a table. Then loop through the grid to get the values and can initialize the DataTable's columns and rows. The attached sample has a Grid Control and a GridDataBoundGrid. The data’s entered in the GridControl are assigned to a DataTable and is displayed in the GridDataBoundGrid.

C#

private DataSet GetDataSetFromGrid(GridControl grid, string tableName, string dataSetName)

{

DataTable dt = new DataTable(tableName);

for(int i = 0; i < grid.ColCount; ++i)

{

string colName = grid[0, i + 1].Text;

if(colName.Length == 0)

colName = GridRangeInfo.GetAlphaLabel(i + 1);

Type t = grid.ColStyles[i + 1].CellValueType;

if(t == null)

t = typeof(string);

DataColumn dc = new DataColumn(colName, t);

dt.Columns.Add(dc);

}

for(int i = 0; i < grid.RowCount; ++i)

{

DataRow dr = dt.NewRow();

for(int j = 0; j < grid.ColCount; ++j)

{

dr[j] = grid[i+1, j+1].CellValue;

}

dt.Rows.Add(dr);

}

dt.AcceptChanges();

DataSet ds = new DataSet(dataSetName);

ds.Tables.Add(dt);

return ds;

}

VB

Private Function GetDataSetFromGrid(ByVal grid As GridControl, ByVal tableName As String, ByVal dataSetName As String) As DataSet

Dim dt As DataTable = New DataTable(tableName)

Dim i As Integer = 0

Do While i < grid.ColCount

Dim colName As String = grid(0, i + 1).Text

If colName.Length = 0 Then

colName = GridRangeInfo.GetAlphaLabel(i + 1)

End If

Dim t As Type = grid.ColStyles(i + 1).CellValueType

If t Is Nothing Then

t = GetType(String)

End If

Dim dc As DataColumn = New DataColumn(colName, t)

dt.Columns.Add(dc)

i += 1

Loop

i = 0

Do While i < grid.RowCount

Dim dr As DataRow = dt.NewRow()

Dim j As Integer = 0

Do While j < grid.ColCount

dr(j) = grid(i+1, j+1).CellValue

j += 1

Loop

dt.Rows.Add(dr)

i += 1

Loop

dt.AcceptChanges()

Dim ds As DataSet = New DataSet(dataSetName)

ds.Tables.Add(dt)

Return ds

End Function

Here is the link with both CS and VB samples: http://websamples.syncfusion.com/samples/kb/grid.windows/GDBGDataset/main.htm

Article ID: Published Date: Last Revised Date: Platform: Control:
478 04/30/2011 07/10/2014 Windows Forms GridDataBoundGrid(Classic)
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon