EquivalentC# coding for Dim dataTable As DataTable = CType(sender, GridView).DataSource - c#-to-vb.net

i am trying to convert Vb.net To C#.net. could any one please help me to find the Equivalent C# coding for Dim dataTable As DataTable = CType(sender, GridView).DataSource.
and also,any tip for soring datas in data gridview.
thanks

You mean like this?:
DataTable dataTable = ((GridView)sender).DataSource;
To cast types in C#, you put the type in parentheses before the value:
(GridView)sender
Then, to access properties on it, you'll want to wrap the whole thing in parentheses:
((GridView)sender).DataSource
(This is because otherwise you'd be trying to call .DataSource on the un-cast sender which would fail.)
Then to declare a value (the variable that you're assigning), the standard syntax is to specify the type and then the variable name:
DataTable dataTable
(I highly recommend using a better variable name, by the way. C# is case-sensitive, so this is valid. But it's unintuitive at best.)
In C# you can also use the var keyword to infer the type, often resulting in cleaner code:
var dataTable = new DataTable();
This only works if there's an inferrable type from the right side of the assignment. Since the DataSource property isn't specifically of type DataTable then you don't want to use var in this particular case, since it would result in an Object (which isn't what you're looking for). But it can be used in cases like my last example above this paragraph where you don't want to repeat the type name twice in the same line of code.

DataTable dataTable = ((GridView)sender).DataSource
Regarding sorting in a GridView, see Sorting Data in a GridView Web Server Control, then post a question with what you have tried if you're having problems.

Related

Winforms ObjectListView: inner OLVColumn instances Name property is empty string so I cannot show/hide columns by name

This question is an offshoot of: Localizing ObjectListView OLVColumn, impossible due to Empty Name property
For simplicity's sake, let's say my ObjectListView contains car information. User A wants to display only Make and Model columns. User B only wants to display Model and Year columns. These preferences would be saved to/loaded from an .ini file on the users' local machines.
I cannot loop through the columns of the ObjectListView and do if (col.Name == colNameFromIni) { col.Visible == true; } because the .Name property of every column is an empty string ("") and does not get serialized to the designer codebehind file. This never happens with any other Winforms control (Label, Button, etc.) They always get their .Name written to the designer codebehind.
In some sense, this is a flaw in Winforms itself, because OLVColumn inherits from System.Windows.Forms.ColumnHeader, and a traditional ListView has exactly the same problem. .Name is always an empty string for all columns.
I would like to patch our local build of ObjectListView.dll to force populate the .Name property, but I can't figure out how Winforms automagically knows the name of every control on the form. It somehow(?) knows the names of the OLVColumn objects since it can display them in the Edit Columns... dialog on the ObjectListView's context menu. I'm also a little fuzzy on where the best spot is to plug this in.
(Yes, per linked question at top I know that as a last resort, I can hardcode colXX.Name = "colXX"; for all columns in my source code, but future column additions are likely to get overlooked and a programmatic solution is much preferred.)
(See also: https://sourceforge.net/p/objectlistview/bugs/160/ : the ObjectListView author declared this a wont-fix so it is up to me (or us), I guess.)
As you point out, this is a bug which is not with the ObjectListView, but the underlying component. And a bug which is around since at least 2008! Therefore, I doubt it will ever be fixed by MS.
Actually, it is a problem with the Autogenerated code in the designer.
If you look at other components such as a button, then the autogenerated code adds a name such as this;
//
// button2
//
this.button2.Location = new System.Drawing.Point(458, 199);
this.button2.Name = "button2";
...
But for ColumnHeader (Listview) and OLVColumn (ObjectListView), then this is not done, so then you end up with this.
//
// olvColumn1
//
this.olvColumn1.AspectName = "Name";
this.olvColumn1.Text = "Name";
If you manually add the line
this.olvColumn1.Text = "olvColumn1";
Then the "problem" is solved.
Of course, you can't do this, because the designer will override the autogenerated code when you make any changes, and then you will lose these manually added lines. It is also not sustainable.
So I'm afraid you need to code around this with some kind of ugly solution. Some options are:
Use the Tag to store the name and compare against this.
Use the text instead of the name (not possible if you have multi
language support!)
Code the names column manually in the Constructor
Set the Text to be something like "ColName;ColText" and then in your
code separate these out.
I have done option 3 in the past, but only I was maintaining the code, so this was easy.
What you could do to ensure you don't have discrepancies is to add a check in your constructor to compare the actual number of columns with the number you expect (hard coded for), and throw an exception if they don't match. Also, not the best, but another way to highlight and reduce errors.
The workaround for this is to get the OLVColumns via reflection and set their column's Name property at runtime. Every OLVColumn is a form-level field, so just pick them out of the list returned by GetFields().
Dim allFieldInfos As FieldInfo() = GetType(FrmMain).GetFields(BindingFlags.NonPublic or BindingFlags.Instance)
For Each fi As FieldInfo In allFieldInfos
If fi.FieldType Is GetType(OLVColumn) Then
Dim instance As OLVColumn = fi.GetValue(Me)
For Each col As OLVColumn In fdlvMain.AllColumns
If ReferenceEquals(col, instance) Then
col.Name = fi.Name
End If
Next
End If
Next

Application of VBA control arrays in Access

I need to understand how I can relate the array of controls I dim in VBA code with the related form and the syntax etc. needed in the code to manipulate the array.
I'm wanting to populate and present an array of textboxes with strings that have been constructed as a result of processing data in a series of tables.
As an initial test, I've tried the following code. I've not yet thought of any way I might attempt to create an array on the surface of a form.
I have several text books on VBA but none of them seem to have anything to say on this.
Can anyone throw any light on this or recommend a more advanced text book?
Dim mytext(20) As TextBox
Dim x As Long
For x = 0 To 19
mytext(x).Value = str(x)
Next x
This results in an error at line 4:
Object variable or With block variable not set
How are you populating your array of Textboxes?
Since Textboxes are objects, you'll need to use Set, e.g.:
Dim mytext(20) As TextBox
Set mytext(0) = Text0
Set mytext(1) = Text2
Set mytext(2) = Text4
Set mytext(3) = Text6
...
Aside, Str is a built-in function in VBA, it should not be used as the name of a
variable.

What is the preferred syntax for declaring an array in VB.NET?

Between the following options, which is the preferred syntax for declaring arrays in VB.NET?
Dim numbers1() As Integer
vs.
Dim numbers2 As Integer()
The MSDN article How to: Initialize an Array Variable in Visual Basic mentions the second option as the preferred syntax: Dim chars2 As Char()..., but the first option seems more common in other documentation. The second option might be confusing since it's so similar to calling a constructor with no arguments.
E.g.
Dim customer As New Customer() ' Initialize a new Customer
which looks similar to:
Dim customer As Customer() ' An uninitialized array of Customers
This question is similar to but different from: Different ways of declaring arrays in VB.NET.
I'll chime in here.
I've seen more of style 1 in code than style two, however there are some minor odd inconsistencies with syntax when it comes to properties.
My theory is the reason the parentheses after is preferred is to make it more consistent with declaring a property that is an array
Public Property x As Integer() 'is an array
Public Property y() As Integer 'is NOT an array
That being said...use Lists :)

How to create an array with textboxes in Visual Basic 6.0

I am working on a very old visual basic project. I have to rewrite the load and save function of the project. Therefore I wanted to create an array of controls which have all relevant textboxes and checkboxes included. I want to iterate through that array to be able to save the data into a textfile or load from it.
I have looked on the internet of how you can define those arrays, but it doesn't seem to work for me. Maybe I am doing something wrong, cause I am not an expert in Visual Basic.
I have tried to make it work this way:
Dim tbList As TextBox = { Form1.Text1, Form1.Text3, _
Form1.Text10, Form1.Text11, Form1.Text12, Form1.Text13, _
Form2.Text1, Form2.Text3, Form2.Text4, Form2.Text5, _
Form2.Text10, Form2.Text11, Form2.Text12, Form2.Text13, _
Form3.Text1, Form3.Text3, Form3.Text4, Form3.Text5, _
Form3.Text10, Form3.Text11, Form3.Text12, Form3.Text13, _
Form3.Text17, Form3.Text18, Form3.Text19, Form3.Text20, _
Form4.Text1, _
Form5.Text1, Form5.Text2, Form5.Text3, _
Form6.Text2, _
Form7.Text2}
Or with a list:
Dim tbList As New List(Of Controls)
The thing is Visual Basic always tells me there are some kind of compiling issues. There is no real explantation for this issue in VB, so I am asking here.
Your code isn't compiling because it is vb.net code. It should go without saying (but I'll say it anyway) that vb6 and vb.net are not the same thing.
If you want to use an array, you will have to dimension the array with a number that is one less than your number of textboxes (if I counted correctly there are 32 in your example):
'// array is zero based so 0 to 31 = 32 items
Dim tbList(31) As TextBox
tbList(0) = Form1.Text1
tbList(1) = Form1.Text3
'//...I'll leave the rest as an exercise for the programmer
tbList(31) = Form7.Text2
Dim i As Integer
Dim tb As TextBox
'// To loop and work with each textbox
For i = 0 To UBound(tbList)
Set tb = tbList(i)
'// do something with tb
Next
An easier way to do it, however, is to work with a collection:
Dim tbList As New Collection
tbList.Add Form1.Text1
tbList.Add Form1.Text3
'//...I'll leave the rest as an exercise for the programmer
tbList.Add Form7.Text2
Dim tb As TextBox
'// To loop and work with each textbox
For Each tb In tbList
'// do something with tb
Next
Yes, you can use a collection if you want to go to the trouble. But an even easier way to work with it is to use VB6's (now obsolete) control array implementation. Most of us were disappointed when we found it was no longer available in .Net!
All you have to do to get control arrays in VB6 is create a bunch of controls with the same name. (They do have to be the same type of control; you can't make arrays of, say, text boxes and combo boxes.) Start with one text box, name it what you want, and copy it. You will be asked if you want to create a control array. Say yes, copy as many as you want. You will notice that the Index property is no longer blank, starting with 0 and incrementing from there. You will also notice that all of the event handlers have an "Index As Integer" argument to them. This way, you can use the same event handler for all of them, evaluating the Index argument to find out which member of your array is firing the event.
Here is the old doc for it. Microsoft makes it hard to find. :)

Initializing DataSets in VB.NET

Is it necessary to use "Nothing" keyword to initialize a DataSet in VB.NET?
Well if you set the variable to "Nothing" then you're not initialising an actual DataSet object at all - you're just setting the value of the variable to a null reference.
For an instance or static variable, Nothing will be the default value. For a local variable, in C# there's effectively no default as the compiler doesn't let you use a variable without it being definitely assigned - whether the VB compiler lets you get away with this or not depends on compiler settings. However, if it does let you use an unassigned variable, the value will be Nothing by default.
Dim ds as New DataSet
(or, if it's already defined, ds = New DataSet) ... is all you need to initialise an actual DataSet object. It's the use of the 'New' keyword that creates the object. Later on, after it has some DataTables in it, you can clear them out with
ds.Tables.Clear
As Jon says,
Dim ds as Dataset = nothing
... does not create an instance of a DataSet, initialised or otherwise. All it does is define 'ds' as a variable that must refer to an instance of the DataSet class, and explicitly sets it to point to nothing.
Dim ds as DataSet
.. would achieve the same thing. Again, later on, after you've used your DataSet,
ds = Nothing
... does not reinitialise the DataSet, all it does is set your variable 'ds' to no longer refer to anything.

Resources