Solved

Converting some vb types to C#/.Net

Posted on 2016-08-03
3
47 Views
Last Modified: 2016-08-10
I'm converting a vb class to C#/.Net. I can't find equivalents for the following:
RecordSet
FieldAttributeEnum
DataTypeEnum

I think RecordSet can be replaced with DataSet, but I don't know about the other two. Any ideas?

Some example code:

    Private Function CreateRecordsetFromDataGrid(ByVal DGV As DataGridView) As Recordset
        Dim rs As New Recordset
        'Create columns in ADODB.Recordset
        Dim FieldAttr As FieldAttributeEnum
        FieldAttr = FieldAttributeEnum.adFldIsNullable Or FieldAttributeEnum.adFldIsNullable Or
                    FieldAttributeEnum.adFldUpdatable
        For Each iColumn As DataGridViewColumn In DGV.Columns
            'only add Visible columns
            If iColumn.Visible = True Then
                Dim FieldType As DataTypeEnum
                'select dataType
                If iColumn.ValueType Is GetType(Boolean) Then
                    FieldType = DataTypeEnum.adBoolean

Open in new window

0
Comment
Question by:allelopath
3 Comments
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
You need to add the COM reference of Microsoft ActiveX Data Objects 2.X Library.

Create ADO Connection and Recordset Objects in Visual C# .NET

Create a new Visual C# .NET Windows application project.
On the Project menu, click Add Reference.
Click the COM tab. Click Microsoft ActiveX Data Objects 2.X Library.

        // You must first load the COM reference Microsoft ActiveX Data Objects 2.X Library

        private ADODB.Recordset CreateRecordsetFromDataGrid(DataGridView DGV)
        {
            ADODB.Recordset rs = new ADODB.Recordset();

            try
            {
                ADODB.FieldAttributeEnum FieldAttr = ADODB.FieldAttributeEnum.adFldIsNullable | ADODB.FieldAttributeEnum.adFldUpdatable;

                foreach (DataGridViewColumn iColumn in DGV.Columns)
                {
                    if (iColumn.Visible)
                    {
                        ADODB.DataTypeEnum FieldType;

                        if (iColumn.ValueType == typeof(Boolean))
                        {
                            FieldType = ADODB.DataTypeEnum.adBoolean;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Don't leave this blank!
            }

            return rs;
        }

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
Comment Utility
My apologies on not being clear in my question. The goal is to move away from ADODB.
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
Comment Utility
Recordet would be quivalent of DataSet/Datatable


I'd suggest something like this:


private DataTable CreateRecordsetFromDataGrid(DataGridView dgv)
{
    Datatable dt = new DataTable();

    Array.ForEach(
    dgv.Columns.Cast<DataGridViewColumn>().ToArray(), 
    arg => dt.Columns.Add(arg.HeaderText, arg.ValueType));
    Array.ForEach(
    dgv.Rows.Cast<DataGridViewRow>().ToArray(), 
    arg => dt.Rows.Add(arg.Cells.Cast<DataGridViewCell>().Select(cell => cell.Value)));

    return dt;
}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now