id pojam opis id pojam opis(duplicate columns)
I am just testing connection from db to c#, and instead of columns id pojam opis I am actually getting id pojam opis id pojam opis in datagridview in c#. Here is part of code which I use to connect database to c#:
public partial class Form1 : Form
{
BindingSource bindingSource1 = new BindingSource();
MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=test;user=root;password=;");
public Form1()
{
this.Load += new System.EventHandler(Form1_Load);
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
ubaciPodatke();
}
public void ubaciPodatke()
{
try
{
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM csharp", conn);
DataTable table = new DataTable();
da.Fill(table);
bindingSource1.DataSource = table; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Is it my mistake, or It has to do something with phpmyadmin: Version information: 4.7.0-rc1. I was thinking it can happen because this version isnt stable yet?
edit: what do You think if I delete this form.resx file
user added rows, would this solve the problem?
Few things:
There is no need to do the data binding in the Load event. You can just do it in the constructor after the InitializeComponent() call. This is irrelevant to your issue.
I don't know what ubaciPodatke means since I don't speak that language, but hopefully it is equivalent of InitializeGrid(). In that case you should put
dataGridView.DataSource = null;
dataGridView.Rows.Clear();
dataGridView.Columns.Clear();
as the first line after try. I do such "clean initialize" since I sometimes design the Grid in designer, for "documentation & visualization" purposes, but do the actual grid from the code. Also, it keeps the InitializeGrid() reusable, to be called from other places.
Also, related to the question in the comments,
Am I reading data from dgv wrong?
You should read the data from the bound DataTable and not directly from the DataGridView when binding.
Solution: I had to comment some lines link1 in form.designer.cs and problem disappeared as shown in this photo link2. I am still curious why this happened as I remember only dragging dgv from toolbox and not adding manually column names.
Related
I have bounded data to datagridview from DataSetand I am trying to filter
these bounded data within datagridview on event of textchange
I got two issues when
I start typing it work fine except it delete the custom datagridview headers and set the name of columns query ex. Header is 'First Name' it replaced by 'NAM' which is the column name at database...
Second issue when I came into else part it wont re-bounded and throw an Exception what I have missed ?
public DataSet GetPatientList()
{
string connStr = ConfigurationManager.ConnectionStrings["SRJDconnstr"].ToString();
string cmdStr = #"SELECT ROW_NUMBER()OVER(ORDER BY ID) AS SEQ,
ID,
DocNUM,
NAM,
FNAME,
LFNAME,
PHONE,
MOBILE,
SEX,
BIRTHDAY,
ADDRESS,
ENDATETIME
FROM SICK
ORDER BY ENDATETIME ASC;";
SqlConnection conn = new SqlConnection(connStr);
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
conn.Open();
cmd.CommandText = cmdStr;
cmd.CommandType = CommandType.Text;
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds, "PatientList");
DGV_PatientList.Columns["DGV_PatientList_RowNum"].DataPropertyName = ds.Tables["PatientList"].Columns["SEQ"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_PatientID"].DataPropertyName = ds.Tables["PatientList"].Columns["ID"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_DocNUM"].DataPropertyName = ds.Tables["PatientList"].Columns["DocNUM"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_FirstName"].DataPropertyName = ds.Tables["PatientList"].Columns["NAM"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_FatherName"].DataPropertyName = ds.Tables["PatientList"].Columns["FNAME"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_LastName"].DataPropertyName = ds.Tables["PatientList"].Columns["LFNAME"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_Phone"].DataPropertyName = ds.Tables["PatientList"].Columns["PHONE"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_Mobile"].DataPropertyName = ds.Tables["PatientList"].Columns["MOBILE"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_Gender"].DataPropertyName = ds.Tables["PatientList"].Columns["SEX"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_Birthday"].DataPropertyName = ds.Tables["PatientList"].Columns["BIRTHDAY"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_Address"].DataPropertyName = ds.Tables["PatientList"].Columns["ADDRESS"].ColumnName;
DGV_PatientList.Columns["DGV_PatientList_EntryDate"].DataPropertyName = ds.Tables["PatientList"].Columns["ENDATETIME"].ColumnName;
return ds;
}
}
Text Change event
private void TB_FirstName_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(TB_FirstName.Text))
{
// first try below
(ds.Tables["PatientList"] as DataTable).DefaultView.RowFilter = string.Format("NAM LIKE '%{0}%'", TB_FirstName.Text);
// second try below
//ds.Tables["PatientList"].DefaultView.RowFilter = string.Format("NAM LIKE '%{0}%'", TB_FirstName.Text);
}
else
{
DGV_PatientList.DataSource = GetPatientList();
DGV_PatientList.DataSource = ds.Tables["PatientList"].DefaultView;
}
}
Set AutoGenerateColumns to false.
That is what is causing the names to change, and also why you are getting the exception. The columns don't exist anymore and you are referencing them by name.
Working with DataGridView bound to a dataset isn't supposed to be this hard - you must have been following a really old, or poorly written tutorial
The way this is all supposed to hang together is much more neat and compact:
//in your code that handles loading the grid with data, e.g. in a Load button handler
patientListTableAdapter.Fill(ds.PatientList); //strongly typed dataset, table is already bound to grid in design time.
//Visual Studio binds it fully for you when you add it to the form, in the designer
//you never again mess with the bindings, just fill and empty the table: MVC
private void TB_FirstName_TextChanged(object sender, EventArgs e){
if(string.IsNullOrWhiteSpace(TB_FirstName.Text)
patientListBindingSource.Filter = null;
else
patientListBindingSource.Filter = string.Format("NAM LIKE '%{0}%'", TB_FirstName.Text);
}
Yes... just 5 lines of code should be doing everything you're trying to achieve here. Right now, you're using these technologies in an incorrect way, and achieving a difficult and poor performing result.
For guidance on how you should be using datatables, refer to MSDN:
https://msdn.microsoft.com/en-us/library/fxsa23t6.aspx
Start with the "Creating a Simple Data Application" walk through, make a new project, follow th steps and create a new sample app. After you've done that, i recommend coming back to the existing app and making NO attempts to salvage what has already been done - delete the lot, remove the datagridview from the form, create a new typed dataset, link it to your DB, drop a new correctly bound datagridview on your form and th designer will set everything up. Then all you need to do is pick a suitable place to load it with data, and apply a textchanged handler (the 5 lines of code I put above)
I know it's going to be hard, throwing away all that code you poured blood weat and tears into.. but it will always be a headache, and never work right, because it's plain up the wrong way to go about working with data and bound controls
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 have a DataGridView populated with a DataSet from my Database. I am trying to make changes in the DataGridView and apply those changes to the Database whenever I press 'Enter'.
Iv read alot of this same question, and researched the topic, but am still having trouble figuring out why I cannot apply changes made in my DataGridView to my Database. (I know this has been asked before, but still cant figure this out).
Can anyone show me what im doing wrong?
DataSet ds = new DataSet();
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter da;
public ListForm()
{
//Setting up DataGridView with data.
InitializeComponent();
da = new SqlDataAdapter("Select * from Contact_List", constring);
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
da.UpdateCommand = cmb.GetUpdateCommand();
da.Fill(ds, "Contact_List");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Contact_List";
}
//Trying to update database with DataAdapter
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
//I believe that the changes to the database should be applied here
//Buts its not working
da.Update(ds, "Contact_List",);
ds.AcceptChanges();
con.Close();
}
}
You should end current edit before trying to save changes:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var cm = dataGridView1.BindingContext[ds, "Contact_List"];
cm.EndCurrentEdit();
da.Update(ds, "Contact_List");
}
Some other notes
You don't need a DataSet. A DataTable is enough.
When you create a SqlCommandBuilder by passing a SqlDataAdapter to the constructor, all insert, update and delete commands will be generated automatically and you don;t need to do anything yourself, so GetUpdateCommand() is not necessary.
Calling EndCurrentEdit cause the changes which you made on an IEditableObject be saved to underlying data source. DataRowView which is the object behind rows of grid is an IEditableObject and you should call EndCurrentEdit of the currency manager which cause EndEdit of DataRowView be called and commits changes to the underlying DataRow and ends the editing session.
If you bind the grid to a BindingSource, calling EndEdit of BindingSource will do the same.
After saving data, you don't need to call AcceptChanges manually.
You need to add exception handling to code.
it's because you're using SqlCommandBuilder, I haven't used it in a long time and I'm looking for more info, but I believe you can only update one table, no joins, and there has to be a unique key defined, otherwise you may want to generate your UPDATE statement manually.
reference
I'm trying to create a refresh button to automatically refresh the data inside my datagridview after i have finish updating them.
However, my refresh button doesn't seem to work. The data displayed remains the same as the original one. It only gets updated after i manually end my windows app and rebuild it.
Here is my code:
private void button_refresh_Click(object sender, EventArgs e)
{
this.acuzioSecureStore_DatabaseXDataSet.AcceptChanges();
}
Please assist. thanks ^_^
The easiest way to handle this is to use a Binding Source object.
If your loading information into your DataGridView from an Access Database then your most likely storing the Data in a Dataset or DataTable.
Create a Binding Source object, and once you have populated your DataTable/Dataset, set the datasource for your Binding Source to your DataTable. Then set the Datasource from the DataGridView as the Binding Source object.
Doing this ensures that any changes in your datagridview or reflected in the DataTable and vice Versa. If you reload data into your DataTable it will reflect in the Data Grid Automatically.
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource= bs;
All changes will now happen automatically.
Hey the solution above is good but when the above code is executed,the table disappears and is not seen. And if I execute
da.Fill(ds, "p");
dataGridView1.DataSource = ds.Tables["p"];
then whole table is created again.
private void button_refresh_Click(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection(#"");
string query="select * from abc";
SqlCommand cmd=new SqlCommand(query,con);
SqlDataAdapter da=new SqlDataadapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
dataGridView1.DataSource=dt;
}
I had a datagridview, bound to a table in an Entity Framework database:
dataGridView1.DataSource = MyDatabase.MyTable;
It would never refresh despite two wasted days.
I solved it with a simple workaround:
private void button_refresh_Click(object sender, EventArgs e) {
dataGridView1.DataSource = MyDatabase.MyTable.Where(i =>(true));
}
This is an ugly workaround, and friend explained me how it works - if I do just dataGridView1.DataSource = database.table, it will cache the table and use the cached data forever. The fact that each time we create a new dummy query, prevents .net from caching it.
Please try this, it worked for me and let me know if you have any better option.
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.Refresh();
}
you Can Bind dataGridView On PageLoad or UserControl Load Event and After chage in grid view Call the Load Event
like
this.ucUsers_Load(null, null); // Windows From C#
I have a base form class that contains a method that returns a DataTable:
protected DataTable GetTableData(string sql, OracleConnection connection)
{
DataTable table = null;
OracleDataAdapter adapter = null;
try
{
table = new DataTable();
adapter = new OracleDataAdapter(sql, connection);
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
}
catch (Exception e)
{
MessageBox.Show("An error occurred while trying to process your request:\n\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (null != adapter)
{
adapter.Dispose();
}
}
return table;
}
Another window is a subclass of it, and invokes it as follows:
private void LoadViewData(OracleConnection connection)
{
DataTable table = null;
try
{
var sql = "SELECT * FROM " + this.ObjectName;
table = GetTableData(sql, connection);
this.resultBindingSource.DataSource = table;
}
catch (Exception e)
{
MessageBox.Show("An error occurred while trying to process your request:\n\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.sqlEditor.Focus();
}
}
resultBindingSource is a System.Windows.Forms.BindingSource. It is set as the DataSource property of a System.Windows.Forms.DataGridView. (The expression, this.ObjectName, evaluates to the name of a table or view in the database.)
When I run through the code in the debugger, I can see that the SQL executes just fine. I can see that the DataTable contains data. I can see that the DataGridView control is properly bound to the data source, and that I can see the data in the data table through its DataSource property. However, no data is displayed in the control itself. There are no row or column headers, and no data is displayed whatsoever.
I have tried everything I can think of to pin down the cause of this problem. This code works exactly as shown on another form. I tried deleting the controls in question and recreating them, to no avail. I consulted the articles on MSDN on how to properly databind to a DataGridView control. I tried it with and without an OracleCommandBuilder (which doesn't seem necessary to me, since this is a read-only view of the data).
I'm frankly out of ideas. It's likely something fairly obvious that I've overlooked. I know that databinding works, because I've done it before with great success.
Any pointers in the right direction would be greatly appreciated.
I tried recreating your program using the pieces you mentioned here. I didn't actually get data from a datatable but that's irrelevant. Here's what I did:
public partial class Form1 : BaseForm
{
BindingSource source = new BindingSource();
public Form1()
{
InitializeComponent();
this.dataGridView1.DataSource = source;
}
private void button1_Click(object sender, EventArgs e)
{
DataTable table = GetDataTable();
this.source.DataSource = table;
}
}
public class BaseForm : Form
{
protected DataTable GetDataTable()
{
DataTable result = new DataTable();
result.Columns.Add("Name");
result.Columns.Add("Age", typeof(int));
result.Rows.Add("Alex", 27);
return result;
}
}
Is this roughly the same thing you have? I had no issues at all. Based on what you're posting this SHOULD work. Are you sure you're binding everything to each other correctly? Post more of your binding code if possible...