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.
Related
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.
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 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 have created a DataGrid and I take the data from database and put it in there.All the data retrieval are done by dynamically in the code behind.Here is a some portion of my code.
SqlConnection con = new SqlConnection("Data Source=PUNUTHL\\SQL;Initial Catalog=mytest;Integrated Security=True");
SqlDataAdapter ad;
DataSet ds = new DataSet();
static DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ad = new SqlDataAdapter("Select * from product",con);
ad.Fill(ds);
dt = ds.Tables[0];
DataGrid1.DataSource = dt;
DataGrid1.DataBind();
}
}
Upto this point it works perfectly.
But I wanted to add the edit function for that DataGrid
But according to my query I took the "product_ID" values as well which can not allows to be updated.
Since I use both "Etid" and "Delete" commands, "prodcut-id" may be my 3rd column as shown in the picture.
So I wanted to be that column read only when the client hit on the edit button.So how can I tackle this problem.Are there any way to get the column names or column indexed?Please can some one help me.
This is one of the way to make a column at any index readonly.
DT.Columns(0).ReadOnly = True '// Make the column(0) readonly and assign to DataGrid.
Datagrid!.DataSource = DT
where DT is datatable used to collect the data from database and is datasource to the datagrid Datagrid1.
For further editing make the column readonly= false and make changes and again turn it into readonly.
Here in your case you can make your Id column that is 2 as readonly
I am trying to make a simple winform application in which i could read/update and insert data to MS Access db table
When i run the application it reads data from MS Access db, and I am able to add new data or edit existing data, but those changes are not sent back to DB.
Code in my Save button click event
Validate();
myBindingSource.EndEdit();
//myTableAdapterManager.UpdateAll(myDataSet.myTable); //this line was in generated code
myTableAdapter.Update(myDataSet.myTable); //replaced previous row with this one, but with no effect
When I press the "save" button
I do not receive any error message, in the DataGridView, new row contains ID with -1 value and new row is not added to database
What could be the problem? What am I missing?
When I opened mdb file from MS Access 2007 it is possible to add a new row to this table
This SO post seems to be about the same problem, but it does not help in my case
unable to add new row using datagridview using C# in Winforms
[EDIT]
I opened .xsd file and added Insert and Update Queries for myTable, but still that does not help - when i press Save button changes are not sent to the database
I found the solution how to send data from Grid control to database, i hope it will help someone else as well (+added some usefull additional stuff)
Here is my code
//form level fields
OleDbConnection conn = new OleDbConnection();
OleDbDataAdapter adapter;// = new OleDbDataAdapter();
DataTable table = new DataTable();
BindingSource bSource = new BindingSource();
//choosing MS Access file
var ecgDbFile = new OpenFileDialog();
ecgDbFile.InitialDirectory = "c:\\";
ecgDbFile.Filter = #"MS Access files (*.mdb)|*.mdb";
ecgDbFile.FilterIndex = 1;
ecgDbFile.RestoreDirectory = true;
//creating connection
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ecgDbFile.FileName;
conn.Open();
//A nice way how to get a list of Data base's tables
var restrictions = new string[4];
restrictions[3] = "Table";
userTableList = conn.GetSchema("Tables", restrictions);
//then loop through and you can get table names => userTableList.Rows[i][2].ToString()
//reading the data (in the grid)
adapter = new OleDbDataAdapter("Select * from "+TableName, conn);
adapter.Fill(table);
bSource.DataSource = table; //binding through bindingsource
GridControl.DataSource = bSource; //actually it works fine if GridControl.DataSource is set to table directly as well...
//choose the appropriate trigger (some gridcontrol or button click event) to call for database updates
private void GridControl_Leave(object sender, EventArgs e)
{
//because of this OleDbCommandBuilder TableAdapter knows how to insert/update database
OleDbCommandBuilder command = new OleDbCommandBuilder(adapter);
adapter.Update(table);
}