I have a GridControl on my windowsform. On a column, I want to show images+text conditionally.
On page load
string command= "select cond, info from Table";
SqlConnection conn = new SqlConnection("Data Source=10.10.10.10;Initial Catalog=zxcv;Persist Security Info=True;User ID=qw;Password=wq");
conn.Open();
SqlDataAdapter adap = new SqlDataAdapter(command, conn);
DataTable dt = new DataTable();
adap.Fill(dt);
gridControl1.DataSource = dt;
conn.Close();
How can I add images on the "cond" column conditionally(ex. if "cond" column is 1 i want to show 1.png+"Condition 1" on the cell).
This is usually handled using a RepositoryItemImageComboBoxEdit. Create one of these and then edit the items so that each of your conditions is represented by one of the Items in the collection. Set your value to match the cond in your datasource and set an image to match the unique cond.
Then set the editor for your cond column to the RepositoryItemImageComboBoxEdit you created. And set the fieldName to the proper field "cond" to complete the binding.
Related
Seems that this is the only query that I don't know, and don't understand. I've set a Query for my Edit Button and here's the code.
ExecuteQuery("Update ICT11 set [Author] = '" & AuthorTxt.Text & "', [Publisher] = '" & PublisherTxt.Text & "', [Subject Code] = '" & SubcodeTxt.Text & "', [Price] = '" & PriceTxt.Text & "', [DiscountAmount] = '" & DiscountTxt.Text & "' Where [Book Name] = '" & BooknameTxt.Text & "'")
Also this is the Module for connecting to the Database
Module SQLDatabase
Public provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Anthony\Desktop\BookSelection\DataBase\BookListUE.accdb"
Public myConnection As OleDbConnection = New OleDbConnection(provider)
Public sql As String
Public cmd As OleDbCommand
Public da As OleDbDataAdapter = New OleDbDataAdapter
Public dt As New DataTable
Function ExecuteQuery(ByVal Query As String) As DataTable
Dim sqlDT As New DataTable
Dim sqlCon As New OleDbConnection(provider)
Dim sqlDA As New OleDbDataAdapter(Query, sqlCon)
Dim sqlCB As New OleDbCommandBuilder(sqlDA)
sqlDA.Fill(sqlDT)
Return sqlDT
End Function
End Module
The functions on the Edit button seem to work fine as I don't encountered any Exceptions. However it doesn't really update the database, together with DataGridView.
You are going about this in completely the wrong way. You use a data adapter to populate a DataTable and bind that to the grid. Any changes the user makes locally should be made to that DataTable.
If they edit via the grid, that happens automatically. If they edit via TextBoxes and the like then those changes can still be pushed to the DataTable automatically if you bind the TextBoxes as well. Otherwise, you should manually update the DataTable with the data from the TextBoxes. Here's an example of binding to both the grid and the TextBoxes:
BindingSource1.DataSource = myDataTable
DataGridView1.DataSource = BindingSource1
TextBox1.DataBindings.Add("Text", BindingSource1, "Column1")
TextBox2.DataBindings.Add("Text", BindingSource1, "Column2")
If you do that then selecting a row in the grid will automatically populate the TextBoxes with the data for that row. Any edits made in the TextBoxes will be reflected in the grid when you navigate to another row. If you weren't binding then you would copy data from the grid to the TextBoxes:
Dim row = DirectCast(BindingSource1.Current, DataRowView)
TextBox1.Text = CStr(row("Column1"))
TextBox2.Text = CStr(row("Column2"))
and then back again:
Dim row = DirectCast(BindingSource1.Current, DataRowView)
row("Column1") = TextBox1.Text
row("Column2") = TextBox2.Text
Note the use of the BindingSource, which is intended to be a one-stop-shop for accessing and manipulating bound data. Note also that, when you bind a DataTable, the data actually comes from its DefaultView property, which is type DataView, rather than its Rows property, which is type DataRowCollection. For that reason, each item is a DataRowView rather than a DataRow. Also, note that you may not be able to use the Current item when copying the changes back, depending on when/where you do it. If the selection has already changed by that point, you will have to remember the row that is being edited by assigning it to a field:
Private editingRow As DataRowView
'...
If editingRow IsNot Nothing Then
editingRow("Column1") = TextBox1.Text
editingRow("Column2") = TextBox2.Text
End If
editingRow = DirectCast(BindingSource1.Current, DataRowView)
TextBox1.Text = CStr(editingRow("Column1"))
TextBox2.Text = CStr(editingRow("Column2"))
You can make as many changes as you like to the DataTable before saving, i.e. you can but you don't have to save after each edit. Once you're ready to save, you use a data adapter - preferably the same one but not necessarily - to save the changes from the DataTable to the database by calling Update. There's no "refreshing" to be done on the grid because it already reflects the changes in the bound DataTable before they are saved.
Here are some examples I wrote some time ago that may help you:
http://www.vbforums.com/showthread.php?469872
http://www.vbforums.com/showthread.php?469518
In all the ADO.NET code in the first thread, note the use of parameters rather than string concatenation to get values into SQL code. To learn why and how to do that, check this out:
http://jmcilhinney.blogspot.com/2009/08/using-parameters-in-adonet.html
I'm creating a form where I can do the following:
please see the image
As you can see, I have a txt_id_up and txt_id_dw
in the database I want to make the following query.
SELECT * FROM Tabla1
WHERE ID BETWEEN 3 AND 7;
where txt_id_up = 3, and txt_id_dw = 7;
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_reader As OleDbDataReader
'------------------------------
'connect to ms.access database
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
connection.Open()
'reading data from Tabla1 table
command = New OleDbCommand("SELECT * FROM Tabla1", connection)
data_reader = command.ExecuteReader
'----------------------------------
'here the code to show in listview1 is missing
'----------------------------------
and in passing I would like to ask another question, can only the following columns be shown in listview?
Name
Account
I clarify that I use the datagridview to see it in general and the listview for queries
I don't know if I get your question but if you want to display Name and Account from your database I suggest you to use DataGridView.
Add a DataGridView control to your form and add this code:
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_adapter As OleDbDataAdapter
'------------------------------
'connect to ms.access database
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
connection.Open()
'reading data from Tabla1 table
command = New OleDbCommand("SELECT Name, Account FROM Tabla1 WHERE ID BETWEEN 3 AND 7", connection)
data_adapter = New OleDbDataAdapter(command)
'add results to DataGridView1
Dim datatable as New DataTable("Table")
data_adapter.Fill(datatable)
DataGridView1.DataSource = datatable
I may have the 2 text boxes backwards.
Public Class Form3
Private Sub FillListView()
ListView1.BeginUpdate() 'keeps the control from repainting on each addition
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
Dim command As New OleDbCommand("SELECT ID, Name FROM Tabla1 Where ID Between ? And ?;", connection)
command.Parameters.Add("FirstID", OleDbType.Integer).Value = CInt(txt_id_up.Text)
command.Parameters.Add("SecondID", OleDbType.Integer).Value = CInt(txt_id_dw.Text)
connection.Open() 'Open the connection at the last possible minute
Using data_reader = command.ExecuteReader
While data_reader.Read()
Dim li As New ListViewItem()
li.Text = CStr(data_reader.GetValue(0)) 'ID
li.SubItems.Add(CStr(data_reader.GetValue(1))) 'Name
ListView1.Items.Add(li)
Loop
End Using
End Using
ListView1.EndUpdate()
End Sub
End Class
Edit
The BeginUpdate of the ListView control prevents the screen from
repainting every time you add an item. This speeds up the addition
of items.
The Using...End Using block makes sure your connection is closed and disposed
event if there is an error.
I added question marks in the Where clause of your SQL statement.
These are placeholders for parameters in the Access/OleDb provider.
I added the 2 parameters to the Parameters collection of the Command
object. The Add method has a number of overloads. The one used here
takes a name and data type. Then the Value property of the parameter
is set to the numbers in the text boxes. These values are in a text
property so they are strings and need the CInt() to convert to
Integers.
I moved the Open method to directly before the command is executed.
Again the Using...End Using block as explained above.
OOPS! I forgot to add the loop (red face) - Code is now corrected
Inside the loop, create a new ListViewItem. A new item for each
iteration of the loop.
Set the Text property of the ListViewItem to the first column in the reader. Convert it to a string. This should show up in the first column.
Set the first SubItem of the ListViewItem to the second column of
the reader. Again convert to a string. This should show up in the
second column.
Add the ListViewItem to the ListView.
The first End Using will close and dispose your data_reader. The
second End Using will close and dispose your connection.
Very Important! ListView1.EndUpdate() Nothing shows up in the
ListView without this line.
Hope this helps.
I have a DataGrid in my UserControl and i am filling it dynamically . Using this code :
conn.Open()
cmd = New OdbcCommand("select dum,I_name from tbl_items where H=1", conn)
Dim da As New OdbcDataAdapter(cmd)
Dim dt As New DataTable("head")
da.Fill(dt)
dgv_itemHead.ItemsSource = New DataView(dt)
conn.Close()
It dose not show data i only shows two lines in it . i have one row in my table ..
Why it is happening ? how to solve this problem
I have a detailsview which is used for inserting new records to the GRIDVIEW. DetailsView has 3 bound fields and one template field. The template field has a dropdownlist control.
When I click insert, all the fields should be entered into the database. Bound fields get inserted, but I have problem with the dropdownlist control. I tried writing this code especially for dropdownlist in "INSERT" click event
protected void btnInsert_Click(object sender, EventArgs e)
{
DropDownList ddl = null;
ddl = new DropDownList();
RateCenters rate = null;
rate = new RateCenters();
rate.statename = ddl.Text;
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.Parameters.Add("#State/Province_name", OleDbType.VarChar).Value = statename;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
I am getting this error : Parameter [State/Province_name] has no default value.
Kindly help me to solve this issue
regards,
Arjun
shouldn't you be using rate.statename
cmd.Parameters.Add("#State/Province_name", OleDbType.VarChar).Value = rate.statename;
I've a grid in my winforms application and i bind a huge dataset to the grid. Will the dataset be stored in the memory by the grid after calling DataBind(). How does it operate on the data binded to the grid?
Update
I wrote the following code
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Server=server;Initial Catalog=db;User ID=testv;Pwd=pass"))
{
con.Open();
using (SqlCommand com = new SqlCommand("select * from tbl_Sample", con))
{
using (SqlDataAdapter ada = new SqlDataAdapter(com))
{
ada.Fill(dt);
dgvMain.DataSource = dt;
dt.Dispose();
}
}
}
After assigning the datatable as the datasource i'm able to dispose it. So does it make a copy in the memory?
Thanks
NLV
It doesn't make a copy, it makes a reference to the original datasource object.
P.S. Making a huge dataset is not a good idea anyway. If you need to display lots of rows, make some kind of paging or filters and restrict number of rows to load and display.