Gridview invalid column name - sql

I have a gridview0 in a multiview and when I click select on a row I have the GridView0_SelectedIndexChanged sub change to a different view in the multiview, which has a different gridview1 but the same datasource as gridview0. This is when it errors out and it displays the invalid column name error, with the column name being the datakeyname of the first gridview0 row that was selected.
The first image is the view of gridview0, and the second is the error that occurs when I click select. Thanks!
image one http://img291.imageshack.us/img291/9221/gridview0.jpg
image two http://img188.imageshack.us/img188/6586/gridview1.jpg
Protected Sub GridView0_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView0.SelectedIndexChanged
Dim ISTag As String = GridView0.SelectedDataKey.Value.ToString
Dim type As String = getTypeMethod(ISTag)
filterText.Text = type
If (type.Equals("Computer")) Then
InventoryComputer.SelectCommand = "SELECT * FROM T_Computer WHERE ISTag = " & ISTag
MultiView1.ActiveViewIndex = 8
End If
End Sub

Adding a new datasource and setting the where to the original gridview worked.

Related

Unable to get a specific Cell value from a Table

I need to port some VBA code to a VB.Net addon, but I can't get a particular cell value from my Table.
I am able to change Range(2,2) to "Test" but table.Range(2, 2).ToString gives System.__ComObject as Value.
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) _
Handles Button1.Click
Dim table As Excel.ListObject
table = Globals.wsTables.ListObjects("vehicleDefinations")
table.Range(2, 2) = "Test"
Dim file As New System.IO.StreamWriter _
("C:\Users\Ryzen2600x\Downloads\Debug.txt", False)
file.WriteLine(table.Range(2, 2).ToString)
file.Close()
End Sub
Don't use .Range to retrieve the value. Instead try to use
table.DataBodyRange.Cells(2, 2)
or
[MyTable].Cells(2,2)
You're missing the .Value
file.WriteLine(table.Range(2, 2).Value.ToString)
Please reference this code:
private void ListObject_Range()
{
Microsoft.Office.Tools.Excel.ListObject list1 = this.Controls.AddListObject(this.Range["A1", "C4"], "list1");
MessageBox.Show("The list object contains " + list1.Range.Cells.Count.ToString() + " cells.");
}
More informaiton, please see
ListObject.Range Property
Help it helps you.
Thanks,
Yuki

How to create group alphabet gridview devexpress?

Can any one help me to create group alphabet gridview to look like this picture?
I've tried with this code, but rows are not grouped by alphabet.
Private Sub GridView1_CustomColumnDisplayText(sender As Object, e As CustomColumnDisplayTextEventArgs) Handles GridView1.CustomColumnDisplayText
If e.Column.FieldName = "CompanyName" AndAlso e.IsForGroupRow Then
Dim rowValue As String = GridView1.GetGroupRowValue(e.GroupRowHandle, e.Column)
Dim val As String = Microsoft.VisualBasic.Left(rowValue, 1)
e.DisplayText = val
End If
End Sub
There is no need to handle the CustomColumnDisplay text event handler to extract the first letter of the "CompanyName" value. Instead, simply set the GridColumn.GroupInterval property for the "CompanyName" GridColumn to "Alphabetical". For instance:
MyGridView.Columns("CompanyName").GroupInterval = ColumnGroupInterval.Alphabetical

TextBox value equals with value from a database

I have a Northwind database and when i want to change or put a new name at a textbox where the names of the ships are, my program to check all the names that already exist in the database and if there is not this name then to show me a message. I created a new table with only the ship names with fill and get commands. I don't know where is my mistake on the code.
Private Sub ShipNameTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ShipNameTextBox.Validating
For i As Integer = 0 To NorthwindDataSet.Orders1.Rows.Count - 1
If Not (ShipNameTextBox.Text = Convert.ToString(NorthwindDataSet.Orders1.Rows(i))) Then
MessageBox.Show("The boat name should be one of the list")
ShipNameTextBox.Focus()
End If
Next
End Sub
You can use DataTable Select() method like below which will return a DataRow[] (assuming that your datatable have a column name shipname)
DataRow[] rows = NorthwindDataSet.Orders1.Select("shipname = " + ShipNameTextBox.Text.Trim())
Then you can just check if the returned DataRow array has any rows. If filter condition doesn't match them it will be empty
if(rows.Length > 0)
{
MessageBox.Show("The boat name should be one of the list")
ShipNameTextBox.Focus()
}

Why does DataGridViewComboBoxColumn display a ValueMember?

I have a small problem. DataGridViewComboBoxColumn displays a value from ValueMember and not from DisplayMember. The grid works fine and when I select something from this column I see the DisplayMember value, but when the focus gets lost, the grid shows a ValueMember. I have this code combo box column:
statusCBoxColumn.DataSource = dt 'datatable with two fields StatusId and StatusText
statusCBoxColumn.DisplayMember = "StatusText" 'is type NVarchar
statusCBoxColumn.ValueMember = "StatusId" 'is type Int
Can anybody help me?
Edit: I solved this in the following way:
Private Sub dgv_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
If (dgv.Columns(e.ColumnIndex).Name = "statusCBoxColumn") Then
If e.Value & "" > "" Then
Dim s1 As String = e.Value
e.Value = GetData("Select StatusText from Status where ID = " & e.Value).ToString()
End If
End If
End Sub
But I don't think that it's the best solution...
Have you set datapropertyName of the comboboxcolumn and added the column to datagridview? You've to write
statusCBoxColumn.DataPropertyName = "StatusId";
and add the column like
datagridview1.Columns.Add(statusCBoxColumn);
I had the same problem.
The "statusID" for me is long.
I made it by system.type.gettype("System.Int64")
That gave the same behaviour.
When changing it to Int32, the behaviour was OK.
So it was à mismatch between valuemember type and datagridview type.
I know this is a 10 years old question, but I was having same problem and I found out that according to MSDN :
When the DataSource property is set to a string array, the ValueMember property does not need to be set because each string in the array will be used as a valid display string and as a valid underlying value.
Try this one I've also have a problem on displaying value member,hope it will work.
For index = 0 To cbo.Items.Count - 1
cbo.SelectedIndex = index
MessageBox.Show(CType(cbo.SelectedValue, String))
Next
What i had used is:
Dim jm As Integer
jm = CType(cmbCategory.SelectedValue, String)
MsgBox(jm)
e.Value = GetData("Select to_char(StatusId) , StatusText from Status where ID = " & e.Value).ToString()

VB 2010 array/write array to file

I'm close to getting this to work, but currently can't get any output to display in the listbox. I had it working, but needed to move some things around to get the join function to work.
In my program, a user enters input into a textbox and an array is displayed in a listbox based on what they type in. For example, if they type in "a", all foods (in the textfile that is connected to the program) that start with "a" will be displayed.
When there is output, I need to find a way to name this array (which is created based on what the user inputs) and join all of the items in the listbox (example: foods stacked on top of each other in the listbox will be shown at the bottom as a string).
I am posting the code that I have thus far; all of the errors that I'm getting (and potentially my logic errors) are just in the first public class until the end of the first if-next statement:
Public Class frmFoods
Dim foods() As String = IO.File.ReadAllLines("foods.txt")
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim Letter As String = txtLetter.Text.ToUpper
Dim smallerarray() As Array
Dim userarray As String
lstOutput.Items.Clear()
If IsNumeric(txtLetter.Text) = False Then
For Each food As String In foods
smallerarray = listfoods(Letter)
lstOutput.Items.Add(Letter)
userarray = Join(smallerarray, ", ")
lstOutput.Items.Add(userarray)
Next
ElseIf IsNumeric(txtLetter.Text) = True Then
MessageBox.Show("Please enter a letter.")
Else
MessageBox.Show("The text box is empty")
End If
End Sub
Function listfoods(ByVal letter As String) As String()
Dim foodarray(foods.Count - 1) As String
Dim counter As Integer = 0
For Each food As String In foods
If food.StartsWith(letter) Then
foodarray(counter) = food
counter += 1
End If
Next
ReDim Preserve foodarray(counter - 1)
Return foodarray
End Function
you need to save the results of the listfoods function in a dictionary or similar and associate it with a key if you want to 'Name' it, although its not clear why you need to do this
As for listing the foods starting with the particular letter, you just need to iterate your result of the function listfoods and separate each one by a comma don't you?
What you have now will create many lists of each food as you are getting the list of food beginning with a particular letter for each food.. As I understand the question you only need to do that once.
Example:
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim Letter As String = txtLetter.Text.ToUpper
Dim smallerarray() As Array
Dim userarray As String
lstOutput.Items.Clear()
If IsNumeric(txtLetter.Text) = False Then
'get all items from the file which begin with the letter the user chose
smallerarray = listfoods(Letter)
'add that letter to the output listbox
lstOutput.Items.Add(Letter)
'join all of the elements which begin with that letter
'into a single comma separated string
userarray = Join(smallerarray, ", ")
'add that string to the output
lstOutput.Items.Add(userarray)
ElseIf IsNumeric(txtLetter.Text) = True Then
MessageBox.Show("Please enter a letter.")
Else
MessageBox.Show("The text box is empty")
End If
End Sub
it would probably be useful for you to step through the code and see the values of the variable at each place and compare this with what you expect to see if you can see where the actual value differs from what your logically expect so you can start to identify where the issue is