I am using Telerik's Radgridview. I currently have it populated with data from a test database and I do this by using a linq query when the window loads. I have two extra empty columns for users to enter in data, and what I want is to have any data entered into those columns be saved back to the database when the window is closed. That way the linq query will still work when I load the application back up again. I'm not entirely sure how to do this or what to put in my Window_Closing event. I've searched nearly every web article and thread on this and none of them have helped me with my particular issue. Specifically I am trying to update the DeductionInfo Table. This is my Window_Loaded event where I have my linq query that loads the data:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Loads queries from each of the designated data tables in BSI_Test
var customerQuery =
(from customer in testEntity.Customers
from deduction in testEntity.DeductionInfoes.DefaultIfEmpty()
join job in testEntity.Jobs
on customer.CID equals job.CID
join claim in testEntity.Claims
on job.JID equals claim.JID
select new DataProperties
{
Customer_Name = customer.CName,
Job_Name = job.JName,
Claim_No = claim.CLNumber,
Check_No = deduction.checkNo,
Check_Date = deduction.checkDate
})
.OrderBy(c => c.Customer_Name);
//Populates the Telerik data grid with data.
gridView.ItemsSource = customerQuery.ToList();
}
This is what I tried in my Window_Closing event which didn't seem to work, and I was wondering if anyone could help me understand how to do this:
DataTable ds = new DataTable();
public void Window_Closing(object sender, CancelEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source = databaseserver; Database = database; User ID = sampleUsername; Password = SamplePassword; Integrated Security = False;");
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM DeductionInfo", con);
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
gridView.ItemsSource = dt;
con.Close();
}
The DeductionInfo table is null by the way, since this data will be used to fill it up.
Related
I'm trying to fill a data grid view in my windows form application but nothing is being returned from the database when I execute the select query. I've looked at other questions about this topic on this site but cannot find anything that addresses my problem.
The name of the data view table is qbcMemDataView and the data source is a sqlite dataset called sqlite_dbDataSet1
Here is the code I have in place:
public Form1()
{
InitializeComponent();
dbConnection = new SQLiteConnection("Data Source=sqlite_db.sqlite;Version=3");
dbConnection.Open();
string[] restrictions = new string[4];
restrictions[2] = "test_table_mom";
using (DataTable dTbl = dbConnection.GetSchema("Tables", restrictions))
{
for (int i = 0; i < dTbl.Rows.Count; i++)
{
tblChooser.Items.Add(dTbl.Rows[i].ItemArray[dTbl.Columns.IndexOf("TABLE_NAME")].ToString());
}
if (tblChooser.Items.Count > 0)
{
tblChooser.SelectedIndex = 0;
}
}
}
private void btnSelect_tbl_Click(object sender, EventArgs e)
{
string sql = "SELECT id, name FROM test_table_mom";
using (SQLiteDataAdapter dbAdapter = new SQLiteDataAdapter(sql, dbConnection))
{
DataTable dataTbl = new DataTable();
dbAdapter.Fill(dataTbl);
qbcMemDataView.DataSource = dataTbl;
}
}
Also, here is a screenshot of the program running that might help better explain the issue I am having: http://imgur.com/j9ffeVi
I know there is data inside the table, I just don't know why it is not appearing in the data grid when the btnSelect_tbl_Click method is executed.
Any help would be appreciated.
Thanks!
Per the tutorial How to: Bind Data to the Windows Forms DataGridView Control, you are missing a BindingSource component that binds the data from the datasource to your table to the DataGrid.
Initialize the BindingSource at the top of your class like so:
private BindingSource bindingSource1 = new BindingSource();
Then near the top of your button click method before the sql add the line:
qbcMemDataView.DataSource = bindingSource1;
and finally change the last line of code
qbcMemDataView.DataSource = dataTbl;
to
bindingSource1.DataSource = dataTbl;
try that and see if it works for you.
Note: I'm not sure if this applies to c# but maybe it's universal fix.
Android builtin adapters and such use _id as the name of the id field. The other problem is _id and id well it's not well documented in android.
About "_id" field in Android SQLite
You can use this technique renaming in the select but it gets messy and you may not catch all occurrences.
string sql = "SELECT id _id, name FROM test_table_mom";
My Opinion: Go back and refactor your db id to _id.
I want to bind the DataGridView to a Table in my Database. I've found several topics about this, but I can't get it to work. Perhaps I'm missing something.
I'm building a "monitor" in which I want to see any Database changes of a specific table. So, after loading the data into a DataGridView, any change in that specific table should be propagate to the DataGridView.
Look what I've done:
SqlDataAdapter daTab;
DataTable dTable;
private void button1_Click(object sender, EventArgs e)
{
string s;
s = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\tempDB\Database1.mdf;Integrated Security=True";
SqlConnection conn= new SqlConnection(s);
conn.Open();
daTab = new SqlDataAdapter("Select * From tab", conn);
dTable = new DataTable();
daTab.Fill(dTable);
BindingSource bSource = new BindingSource();
bSource.DataSource = dTable;
dgv1.DataSource = bSource;
}
But it only updates the DataViewGrid when I run
daTab.Fill(dTable);
So I may have two options from here: 1) get it to work right (this is why I'm needing your help) or 2) put a timer and run daTab.Fill(dTable); at some time interval. I am particullary looking after option 1). So, I'd appreciate any help given.
It sounds like you're using SQL Server. If so, have a look at the SqlDependency class. You can create a delegate for this class's OnChange event and be notified of changes, which you can then propagate to your GridView. Here is a good writeup on the topic with some examples:
https://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx
I am currently working on a database system. In the system a user can search for a specific member using their ID. Searching for them filters all DataGridView results to just that specific member.
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.ReadOnly = false;
using (SqlConnection con = new SqlConnection(constring))
{
int id = Convert.ToInt32(textBox1.Text);
con.Open();
DataTable FindAaron = new DataTable();
SqlDataAdapter adapt = new SqlDataAdapter("SELECT * FROM MembersTable WHERE MemberID =" + id, con);
adapt.Fill(FindAaron);
dataGridView1.DataSource = FindAaron;
con.Close();
}
}
This code filters the DataGridView results down to one row from the table 'MembersTable'. The user can now physically click on the table cell and edit the data as much as they want. Once they are finished they hit a 'Save Changes' button which I want to save the changes they made, update the source table and refill the DataGridView with all the members, now with updated info. This is the code I have behind the 'Save Changes' button at the moment.
try
{
//MemberClass.UpdateMember();
this.membersTableTableAdapter.Update(mainDatabaseDataSet.MembersTable);
dataGridView1.Refresh();
MessageBox.Show("Details updated");
}
catch
{
MessageBox.Show("An error has occured");
}
This unfortunately does not update the DataGridView in the form to display all the updated data or save the data that has been edited back to the Sql table. Have puzzled over this for a few days and can't figure out what I'm doing wrong. Any and all help is much appreciated.
Actually there is no connection seen between membersTableTableAdapter and adapt or mainDatabaseDataSet.MembersTable and FindAaron.
Try as following;
//Get the changed data
DataTable changes = FindAaron.GetChanges();
if (changes != null)
{
//Update data
adapt.Update(changes);
}
I am attempting to build a simple application that allows me to interact with a MySQL database I have set up. I can add records just fine; however, I cannot get them to display. I am simply presented with a blank datagrid control.
It could be something simple, but I have tried different approaches, searched online, and messed with it longer than I should have - to no avail. I have a feeling the issue might lie in my lack of understanding the DataGrid control. Any help is sincerely appreciated.
The Select method from the Database Connection class:
public DataTable Select(string tableName)
{
string query = "SELECT * FROM " + tableName;
this.Open();
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
this.Close();
return dataSet.Tables[0];
}
The following is a portion of the code for the window (called View) containing the datagrid. The table name is passed to it from the main window upon the click of a button.
public View()
{
InitializeComponent();
}
public View(string table)
{
this.table = table;
InitializeComponent();
}
private void windowLoad(object sender, RoutedEventArgs e)
{
DatabaseConnection myConnection = new DatabaseConnection();
dataGrid.DataContext = myConnection.Select(table).DefaultView;
}
I'm trying to create a two-datagridview Master-Detail form to display the results of an SQL statement, but appear to have an added complication in that the two results I want to display are from the same table: I want just the IDs to appear in the top grid, and then detail from the rest of the table about the same ID to appear in the bottom grid. If I'm using the wrong method, then could someone please point me in the wrong direction?
My current problem is that the code gets to the DataRelation Rel = new DataRelation.... and displays the form (with both DataGrids blank) at that point, rather than executing the rest of the code.
I've pasted the full version below:
private void Form1_Load(object sender, EventArgs e)
{
SpContain.Panel1.Controls.Add(masterDataGridView);
SpContain.Panel2.Controls.Add(detailsDataGridView);
masterDataGridView.DataSource = masterBindingSource;
detailsDataGridView.DataSource = detailsBindingSource;
GetData();
}
private void GetData()
{
string ConnStr = "Server=TradarUAT\\uattradar; Integrated Security=SSPI; Initial Catalog=TradeFiles;";
SqlConnection Conn = new SqlConnection(ConnStr);
DataSet Data = new DataSet();
Data.Locale = System.Globalization.CultureInfo.InvariantCulture;
SqlDataAdapter masterDataAdapter = new SqlDataAdapter("Select MasterReference from [TradeFiles].[dbo].[OMG-Rejects] GROUP BY MasterReference", Conn);
masterDataAdapter.Fill(Data, "[TradeFiles].[dbo].[OMG-Rejects]");
SqlDataAdapter detailDataAdapter = new SqlDataAdapter("Select ImportedDT,TypeIndicator,FileNumber,MasterReference,ClientAlocRef,VersionNumber,DateTimeStamp,ErrorID,ErrorKey,ErrorTxt,ErrorParamType,ErrorParamValue from [TradeFiles].[dbo].[OMG-Rejects]", Conn);
detailDataAdapter.Fill(Data, "[TradeFiles].[dbo].[OMG-Rejects]");
DataRelation Rel = new DataRelation("RejectDetail",
Data.Tables[0].Columns["MasterReference"], Data.Tables[1].Columns["MasterReference"]);
Code stops executing on the above line
Data.Relations.Add(Rel);
masterBindingSource.DataSource = Data;
masterBindingSource.DataMember = "[TradeFiles].[dbo].[OMG-Rejects]";
detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "RejectDetail";
}
Managed to solve this by creating a view on the SQL server that mimicked the first select statement I was trying to acheive. Then I just referenced the view in the masterDataAdapter, and retrieved the rest of the information from the underlying table in the detailDataAdapter.