Here I am trying to assign the datasource (using same code given in the sample application) and create a graph, only difference is i am doing it in WPF WindowsFormsHost. due to some reason the datasource is not being assigned properly and i am not able to see the series ("Series 1") being created. wired thing is that it is working in the Windows Forms application but not in the WPF one.
am i missing something and can somebody help me?
Thanks
<Window x:Class="SEDC.MDM.WinUI.WindowsFormsHostWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr- namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.Dat aVisualization"
Title="HostingWfInWpf" Height="230" Width="338">
<Grid x:Name="grid1">
</Grid>
</Window>
private void drawChartDataBinding()
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
string fileNameString = #"C:\Users\Shaik\MSChart\WinSamples\WinSamples\data\chartdata.mdb";
// initialize a connection string
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// define the database query
string mySelectQuery = "SELECT * FROM REPS;";
// create a database connection object using the connection string
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
Chart Chart1 = new Chart();
// set chart data source
Chart1.DataSource = myCommand;
// set series members names for the X and Y values
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
// data bind to the selected data source
Chart1.DataBind();
myCommand.Dispose();
myConnection.Close();
host.Child = Chart1;
this.grid1.Children.Add(host);
}
Shaik
With the following two changes, you can fix your code:
1 - Instead of
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
write
Chart1.Series["Series 1"].XValueMember = "Name";
Chart1.Series["Series 1"].YValueMembers = "Sales";
2 - Before the above code, insert the following lines:
Chart1.ChartAreas.Add("Default");
Chart1.Series.Add(new Series("Series 1"));
Related
first of all thanks for taking your time, now
the problem i have is this;
I have different textblocks in my main XAML file, im trying to get their text to use them in a sqlquery from another file(.cs file)
public void FillTable()
{
MainWindow l = new MainWindow();
string sql = "insert into Pacientes (nombre) values ('"+ l.nombre_text.Text +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
l.Close();
}
However, when i check the Table the result is null, when i check the table the
"nombre" column is blank
See sql image
any clue what im doing wrong?
You can't do a "new MainWindow". You need to get a reference to your MainWindow or somehow pass in the text to FillTable().
Without seeing more of your code, an exact solution is not probable, but something along these lines might get you unblocked.
...
// in MainWindow.cs
FillTable(this);
...
public void FillTable(MainWindow window)
{
string sql = "insert into Pacientes (nombre) values ('"+ window.nombre_text.Text +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
OR
...
FillTable(nombre_text.Text);
...
public void FillTable(string nombre)
{
string sql = "insert into Pacientes (nombre) values ('"+ nombre +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
Currently when I load my program Bing Maps will only load the first pushpin onto the map, for my example I have 4 pushpins which should be displayed when the application is loaded, what additional code would I add in order to make it complete all four.
In addition I have a couple of questions if you don't mind answering
Do I need to use a loop for each location?
Do I have to give each one an individual name? (Pin)
Can I link a access database instead of copying the locations across?
Is it possible to hide or remove pushpins when a button is clicked?
Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
UserControl11.BingMap.Children.Add(Pin)
Pin.Location = (New Location(55.852663, -2.3889276))
Pin.Location = (New Location(55.956023, -3.1607265))
Pin.Location = (New Location(54.840279, -3.2886766))
Pin.Location = (New Location(52.819511, -1.8851815))
If it is just these 4 pins you want to create, then you can use the following code:
Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
Pin.Location = (New Location(55.852663, -2.3889276))
UserControl11.BingMap.Children.Add(Pin)
Dim Pin2 = New Microsoft.Maps.MapControl.WPF.Pushpin()
Pin2.Location = (New Location(55.956023, -3.1607265))
UserControl11.BingMap.Children.Add(Pin2)
Dim Pin3 = New Microsoft.Maps.MapControl.WPF.Pushpin()
Pin3.Location = (New Location(54.840279, -3.2886766))
UserControl11.BingMap.Children.Add(Pin3)
Dim Pin4 = New Microsoft.Maps.MapControl.WPF.Pushpin()
Pin4.Location = (New Location(52.819511, -1.8851815))
UserControl11.BingMap.Children.Add(Pin4)
Alternatively, if your location data is changing or you have an array/list of location information you can loop through, create pushpins and add them to the map like this:
Dim myLocations(4) As Location
myLocations(0) = New Location(55.852663, -2.3889276)
myLocations(1) = New Location(55.956023, -3.1607265)
myLocations(2) = New Location(54.840279, -3.2886766)
myLocations(3) = New Location(52.819511, -1.8851815)
For index = 0 to myLocations.Length - 1
Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
Pin.Location = myLocations(index)
UserControl11.BingMap.Children.Add(Pin)
Next
help, combobox just keep adding items, i tried using removeallitems but after that i cant put anything on the first combobox
public class Function {
public void combofillsect(JComboBox section, String year){
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String query;
try{
query = "Select Section from asd where Year=?";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
pst = conn.prepareStatement(query);
pst.setString(1, year);
rs = pst.executeQuery();
while(rs.next()){
section.addItem(rs.getString("Section"));
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
section.addItem(e.toString());
};
}
Function funct= new Function();
{funct.combofillsect(jComboBox1,String.valueOf(jComboBox2.getSelectedItem())); }
why cant I post image?
Are you programming in C# ? If it's the case then you can use the function Clear like that : yourComboBox.Items.Clear() to delete all the current items. I don't know if it will solve your problem but your technique of getting the data from your database seems weird to me, if you used a dataset you could have done dataset.Tables(0).Rows.Count() to get the number of entries and then set the exit condition of your loop like this -> counter < dataset.Tables(0).Rows.Count(), and set a counter++ at the end of your while (maybe that's why you say your combobox won't stop filling, but I don't know what do the next() function).
I don't know the C# code but there is my VB.NET function :
Public Function getAll() As DataSet
ConnectionDB()
Dim cmd As SqlClient.SqlCommand
cmd = New SqlClient.SqlCommand("SELECT * FROM table", Connect)//Connect is a System.Data.SqlClient.SqlConnection, or my connection string
Dim adapter As New Data.SqlClient.SqlDataAdapter
Dim dataset As New DataSet
adapter.SelectCommand = cmd
adapter.Fill(dataset)
adapter.Dispose()
cmd.Dispose()
Connect.Close()
Return dataset
End Function
I don't know if I helped you but I didn't really understood what your problem was and you didn't even mentioned the language you use ^^ Good luck
Edit : and if you can't post images, that's because you don't have yet 10 points of reputation, you can get informations about reputation here : https://stackoverflow.com/help/whats-reputation, but you still can post the link of a picture it will allow users to click on it
There is a DataGridView which is created at runtime with data fetching from database. There are four types of columns in data grid view:
DataGridViewTextBoxColumn
DataGridViewComboboxColumn
DataGridViewButtonColumn
DataGridViewCheckBoxColumn
Code for creating that datagridview is as below:
_form.DGV_.AutoGenerateColumns = False
_form.DGV_.Columns.Clear()
'Required Variables
Dim pom_DataGridViewTextBoxColumn As DataGridViewTextBoxColumn = Nothing
Dim pom_DataGridViewComboBoxColumn As DataGridViewComboBoxColumn = Nothing
Dim pom_DataGridViewButtonColumn As DataGridViewButtonColumn = Nothing
Dim pom_DataGridViewCheckBoxColumn As DataGridViewCheckBoxColumn = Nothing
'Column-1 (ID: jntDate)
pom_DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
pom_DataGridViewTextBoxColumn.DataPropertyName = "jntDate"
pom_DataGridViewTextBoxColumn.HeaderText = "Date"
pom_DataGridViewTextBoxColumn.DefaultCellStyle.Format = "d"
_form.DGV_.Columns.Add(pom_DataGridViewTextBoxColumn)
'Column-2 (ID: jntAcc)
pom_DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn
pom_DataGridViewComboBoxColumn.DataPropertyName = "jntAcc"
'Cond: If dataset Acc does not contain any table then add table from database.
If Acc.Tables.Count > 0 Then
pom_DataGridViewComboBoxColumn.DataSource = Acc.Tables.Item(0)
Else
Acc.Tables.Add(Tbl_Select("SELECT *, accNo AS byNo, accName as byName FROM tblAccounts", False, ""))
pom_DataGridViewComboBoxColumn.DataSource = Acc.Tables.Item(0)
End If
'Cond: If _Account_Name_Number is True then set DisplayMember property to 'byName'.
If _Account_Name_Number Then
pom_DataGridViewComboBoxColumn.DisplayMember = "byName"
Else
pom_DataGridViewComboBoxColumn.DisplayMember = "byNo"
End If
pom_DataGridViewComboBoxColumn.ValueMember = "accID"
pom_DataGridViewComboBoxColumn.HeaderText = "Account"
_form.DGV_.Columns.Add(pom_DataGridViewComboBoxColumn)
'Column-3 (ID: xxxx)
pom_DataGridViewButtonColumn = New DataGridViewButtonColumn
pom_DataGridViewButtonColumn.DataPropertyName = "xxxx"
pom_DataGridViewButtonColumn.UseColumnTextForButtonValue = True
pom_DataGridViewButtonColumn.HeaderText = "X"
pom_DataGridViewButtonColumn.Text = "X"
pom_DataGridViewButtonColumn.Width = 32
pom_DataGridViewButtonColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
pom_DataGridViewButtonColumn.DefaultCellStyle.ApplyStyle(pom_DataGridViewButtonColumn.DefaultCellStyle)
_form.DGV_.Columns.Add(pom_DataGridViewButtonColumn)
'Column-4 (ID: jntReview)
pom_DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn
pom_DataGridViewCheckBoxColumn.DataPropertyName = "jntReview"
pom_DataGridViewCheckBoxColumn.HeaderText = "Review"
pom_DataGridViewCheckBoxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
pom_DataGridViewCheckBoxColumn.DefaultCellStyle.ApplyStyle(pom_DataGridViewCheckBoxColumn.DefaultCellStyle)
_form.DGV_.Columns.Add(pom_DataGridViewCheckBoxColumn)
In WPF application, there is a need to populate same DataGrid with these four columns.
How can I create and bind data to four types of columns in DataGrid in WPF ?
Why not skip doing all that stuff in cs code? You'll start pinning yourself in the corner and WILL hit the wall when the datagrid will need more complex templates, data triggers, custom styles, animations, etc... Besides, it will make it easier not only on the next person, but you as well. When you have to add/fix something in the beast you create in .cs code 3-6 months down the road, it might take you quite a while to understand what is going on :)
Instead, do all your work in xaml, Create a UserControl, put this datagrid in it, Pass the DataContext from various instances of the UserControl down to it's DataGrid.
I have a WPF User Control with a ListView in it that is created based on the DataSet that is passed to it:
public void PopulateList(DataSet ds) {
listView.View = CreateGridViewColumns(ds.Tables[0]);
listData.DataContext = ds.Tables[0];
}
private GridView CreateGridViewColumns(DataTable dt) {
// Create the GridView
var gv = new GridView {AllowsColumnReorder = true};
// Create the GridView Columns
foreach (DataColumn item in dt.Columns) {
var gvc = new GridViewColumn
{
DisplayMemberBinding = new Binding(item.ColumnName),
Header = item.ColumnName,
Width = Double.NaN
};
gv.Columns.Add(gvc);
}
return gv;
}
Now I create the user control in code and call it's PopulateList with the appropriate dataset and this is where the problems are starting:
If I pass in a dataset that was created from a call to the database the list view shows all the data but if i pass in a DataSet that i created in code the ListView shows the Columns but will not show the data
//This is a function that hides the DB call return type is DataSet
var dsPatientSmokingStatusHistory = DataRepository.PatientSmokingStatusProvider.GetHistory(PatientId);
//radGridViewPatientSmokingStatus.DataSource = dsPatientSmokingStatusHistory.Tables[0];
var dt = new DataTable();
string c1 = "Date".PadLeft(23).PadRight(23);
string c2 = "Status".PadLeft(20).PadRight(50);
dt.Columns.Add(c1);
dt.Columns.Add(c2);
int i = 0;
foreach (DataRow row in dsPatientSmokingStatusHistory.Tables[0].Rows) {
var dataRow = dt.NewRow();
dataRow[c1] = ((DateTime)row["Date"]).ToString("MM/dd/yyyy");
dataRow[c2] = row["Status"].ToString();
dt.Rows.Add(dataRow);
dt.Rows[i].AcceptChanges();
i++;
}
DataSet ds = new DataSet();
dt.TableName = "Table";
ds.Tables.Add(dt);
ds.AcceptChanges();
smokingStatusGrid.GridWidth = 455;
smokingStatusGrid.GridHight = 97;
//This line does not show data
smokingStatusGrid.PopulateGrid(ds);
//This line will show data
smokingStatusGrid.PopulateGrid(dsPatientSmokingStatusHistory);
Is there a difference between these two datasets that i don't know about that is preventing me from databinding to it?
Also the user control is being used as an ElementHost in a WinForms application (not sure if this makes a difference)
Your code says:
DisplayMemberBinding = new Binding(item.ColumnName)
This binding constructor takes a string paramter which as per MSDN is "The initial Path for the binding" and is of datatype System.Windows.PropertyPath. I guess, since system tries to find a property with the same name in your class, and your string (item.ColumnName) has spaces at start, it runs into a problem (properties can't start with a space).
Would recommend you to take off the padding that you are doing in column name of your table. Apply any padding/margins in the Header of your GridView.