This shouldn't be this hard... But it's late.
I am working on a simple form, and trying to delete a record from a connected DataSource while using a TableAdapter. Here is the SQL for the TableAdapter;
DELETE FROM Main WHERE (ID = ?) AND (tbl_Job_Name = ?)
Main is the table name, only two fields.
I am populating a ComboBox with this data, and I am using a Button to call the Delete() action like this;
Private Sub btnDeleteJob_Click(sender As Object, e As System.EventArgs) Handles btnDeleteJob.Click
Dim deleteJobAdapter As New DCGDataSetTableAdapters.MainTableAdapter
deleteJobAdapter.DeleteQuery(ComboBox2.SelectedIndex, ComboBox2.SelectedText)
End Sub
When I break the code I can see the ID value, but the SelectedText field is blank, and of course when it runs through the record is not deleted. I would ideally like to just pass the ID of the selected record in the ComboBox to delete the record. What am I missing?
Try,
deleteJobAdapter.DeleteQuery(ComboBox2.SelectedIndex, ComboBox2.Text)
SelectedText property:
Gets or sets the text that is selected in the editable portion of a ComboBox.
Text property:
Gets or sets the text associated with this control.
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.
When I enter the code;
deleteJobAdapter.DeleteQuery(ComboBox2.SelectedIndex, ComboBox2.Text)
My break point shows the correct text from the record, but I did notice that the ID value is incorrect, the SelectedIndex returns a sequential number of the record itself, starting with "0". So it looks like SelectedIndex does not return the actual ID value...
And back to the original issue, the record selected is still not deleted.
This is what I ended up using;
Dim delJobID = ComboBox2.SelectedValue
Dim delJobRowAdpt As New DCGDataSetTableAdapters.MainTableAdapter
Dim delJobRow As DCGDataSet.MainRow
Dim intDelete As Integer
delJobRow = DCGDataSet.Main.FindByID(delJobID)
delJobRow.Delete()
Related
I have an application with a datagrid view
This datagrid is filled by code, not binded to a datasource.
All cells are editable
when a user edits a value in a cell, then i need to perform an action
Private Sub dgView_CellValidated(sender As Object, e As DataGridViewCellEventArgs)
Handles dgView.CellValidated
Dim nColumnIndex As Integer
DIm nRowIndex as Integer
If sender.iscurrentRowDirty Then
nRowIndex = e.RowIndex
nColumnIndex = e.ColumnIndex
Call UpdateRowData(nRowIndex, nColumnIndex)
End If
End Sub
The Function UpdateRowData is called when user edits en then leave the cell, but if he goes to a cell in the same row. Then this routine will be called again when the user again goes to another cell without any editing
I want this function only called once
How are you going to know if the cell’s value had “actually” changed from its original value? And what is this “action” that is taken if the cell “was” changed? Is what I am getting at… is that it is unclear “what” this “action” is doing? If it is an extensive process that may take some time and you want to avoid this “action” if the cells value doesn’t “actually” change. Then you will need to know if a cells value actually DID change. And this will require a different event and some additional code implementation on your part.
On the other hand, if the “action” is simply to update something with the new value, then it may take MORE execution steps to actually “check” if the value has changed as opposed to simply overwriting the same values. So, if the “action” is trivial, I wouldn’t bother “checking” if the values have changed.
However, if you DID need to make sure that the user “actually” did change the cells value… Then below is a crude, yet simple, solution for this.
One possible issue is if the user clicked into a cell and edited it by “actually” typing some characters, but, after the user finishes typing characters they had ended up simply re-typing what the original value was… then “technically” from the grids perspective… THAT cells value DID change. In other words, We will need to somehow get the original value of the cell “BEFORE” the user starts to edit the cell. Then we could “compare” the new value with the original value when the user tries to leave the cell.
Fortunately, there is a grid event that should make this fairly trivial to implement. The event I suggest using is the grids CurrentCellDirtyStateChanged event. This event will actually fire TWICE. The event will fire once when the cell goes into “edit” mode. And it will fire again when the user ends the same cells edit mode.
Therefore, we can take advantage of this, however, we will need to know that WHEN the event fires… is it fired when the user “enters” the cells edit mode… OR … is the event fired because the user is “ending” the cells edit mode. I do not think the event keeps track of this internally so we need to create our own “mechanism” to be able to distinguish when the event is fired on the “begin” edit and the “end” edit.
One possible solution to make this work is to create two global variables… one a bool variable called FirstIn and a string variable called OriginalValue… When the event fires the first time to “begin” the cells edit mode, we can check the FirstIn variable. If it is true then we know the cell is “beginning” the edit of the cell… this is where we would capture the cells current value and set the OrignalValue string variable so we can “check” it against the final cells value when the user ends the cells edit mode.
Capturing the current cells value and setting FirstIn to false is all we need to do. We now wait until the user ends the cells edit mode and the event fires for the second time. When it fires the second time… again we check the FirstIn variable and in this case it is false which means the cells is ending its edit mode. Here we could check the OriginalValue with the cells current value and do your “action” if they are different. And finally setting FirstIn to true to start the whole process over.
Below is an example of what is described above.
bool FirstIn = true;
string OriginalValue;
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) {
if (FirstIn) {
FirstIn = false;
OriginalValue = dataGridView1.CurrentCell.Value.ToString();
}
else {
string NewValue = dataGridView1.CurrentCell.Value.ToString();
FirstIn = true;
if (OriginalValue.Equals(NewValue)) {
MessageBox.Show("NO changes were made in the cell edit");
}
else {
MessageBox.Show("YES changes were made in the cell edit");
}
}
}
Sorry but I wrote this in C#, below is a VB version.
Dim FirstIn As Boolean = True
Dim OriginalValue As String
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
If (FirstIn) Then
FirstIn = False
OriginalValue = DataGridView1.CurrentCell.Value.ToString()
Else
Dim NewValue As String = DataGridView1.CurrentCell.Value.ToString()
FirstIn = True
If (OriginalValue.Equals(NewValue)) Then
MessageBox.Show("NO changes were made in the cell edit")
Else
MessageBox.Show("YES changes were made in the cell edit")
End If
End If
End Sub
I have a DataGridView with 3 columns.
I need the first two columns remain constant, These columns always have to be the same and the user will not be able to enter a different value to these.
The values for the two first columns of the DGV are in textBox1 and textBox2.
I need that when the user is going to add a new row in the DGV the first two columns automatically fill with these constant values and set the focus to the third column.
Thanks in advance
If you don't want the user to edit the first two columns, just set their ReadOnly property to true:
this.dataGridView1.Columns[0].ReadOnly = true;
As for your other criteria, first set the following property to limit control of when a new row is added (ex. click of a button):
this.dataGridView1.AllowUserToAddRows = false;
Then create your own method to add new rows when needed:
private void AddNewRow()
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
row.Cells[0].Value = this.textBox1.Text;
row.Cells[1].Value = this.textBox2.Text;
this.dataGridView1.CurrentCell = row.Cells[2];
this.dataGridView1.BeginEdit(true);
}
Technically, you could do this when AllowUserToAddRows == true by handling the DataGridView.RowsAdded event and the above code slightly modified, but you'll get an annoying behavior where as soon as you enter one character into the new row, 3rd column, another new row is added and the editing cell loses focus (probably before you actually entered anything useful).
I have a telerik RadGrid that gets populated with data from a SQL database when the grid loads. The first column lists a name, which needs to be a hyperlink to another part of the website. I have tried a couple different options, neither of which gets me the results I need.
The first way I tried was using a GridHyperLinkColumn. However that does not allow me to change the displayed text of the hyperlink programmatically when the grid gets populated with data.
<telerik:GridHyperLinkColumn DataNavigateUrlFields="joblink" DataNavigateUrlFormatString="/Job.aspx?id={0}" Text="JobName">
taskDR.Item("joblink") = dataReader("publicID")
taskDR.Item("joblink").Text = dataReader("name") 'This is what I would like to do
The other option was to use a GridBoundColumn and bind a Hyperlink to it.
<telerik:GridBoundColumn DataField="joblink" UniqueName="joblink">
Dim jobhyperlink As New HyperLink()
jobhyperlink.Text = dataReader("name")
jobhyperlink.NavigateUrl = "/Job.aspx?id=" & dataReader("publicID").ToString()
taskDR.Item("joblink") = jobhyperlink
However instead of displaying the hyperlink in the joblink column, all that displays is "System.Web.UI.WebControls.HyperLink"
I looked into the DataTextField and DataTextFormatString properties of GridHyperLinkColumn, but I couldn't find a way to alter those fields programmatically.
you can try a template column and just place a html tag and use Eval function to bind the things the way you want.
Check out this demo - http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx
I finally figured it out.
<telerik:GridHyperLinkColumn UniqueName="joblink" Text="jobname" DataNavigateUrlFields="joblink" DataNavigateUrlFormatString="/Employer/Job.aspx?action=edit&id={0}" DataTextField="jobname" DataTextFormatString="{0}">
Dim taskDT As New DataTable
taskDT.Columns.Add("jID")
taskDT.Columns.Add("jobname") 'You need one column for the DataTextField
taskDT.Columns.Add("joblink") 'and another for the DataNavigateUrlField
While dataReader.Read()
Dim taskDR = taskDT.NewRow()
taskDR.Item("jobname") = dataReader("name")
taskDR.Item("joblink") = dataReader("publicID")
I want to export the contents of a combo box to the local data table so that I can parametrize the test using that value.
You need to get each item from the ComboBox. For example where Window Name and ComboBox Name are the names of your window and ComboBox respectively
comboBox = Window("Window Name").WinComboBox("ComboBox Name");
count = comboBox.GetItemsCount
For i = 0 to count-1
item = comboBox.GetItem(i)
' put the item in the DataTable...
Next
How you output each item, the ' put the item in the DataTable.. line, will depend on your set up.
I have 2 forms (2 windows). In first window,I have a tree view (the children are the tables in DB.). On click of a any child, a list view (the data present in the table ) will be displayed on the right side of the tree view.
On double clicking on any row in the list view, another form will be opened. The current requirement is to display the data in the list-view in form2 opened on double clicking a single record.
Private Sub LV_DblClick()
Dim a As New Form2
Display_Temp_DATA
a.Show vbModal
End Sub
LV_DblClick() is in the form1 and it opens the form2. Now, Display_Temp_DATA has SQL query, which
fetches the record from the table and should be displayed in a list view in form2.
Private Sub Display_Temp_DATA()
On Error GoTo errHandler
'Dim liItem As ListItem
'Set liItem = a.ListView1.ListItems.Add(, , "Abcd")
Display_List_Two_Data "Select start, stop FROM tblsignal"
Exit Sub
errHandler:
MsgBox Err.Description
End Sub
I am able to open the form2, but I am finding difficult to display the data in the form2 list view. Any ideas on how to resolve this?
you can add module and variable to it and assign the value to that variable access it on the target form.
not sure but you can also use FormName.VariableName for static variable on the form.