Articles in this section
Category / Section

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

3 mins read

 

In WinForms GridDataBound, 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: https://websamples.syncfusion.com/samples/kb/grid.windows/GDBGDataset/main.htm


Conclusion

I hope you enjoyed learning about how do I create a DataSet with the data entered in the WF Grid.

You can refer to our WinForms Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Grid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied