I'm trying to use a datagridview for displaying several different tables data upon a radio click event. My problem is that the table adapter is not showing up but when I check the .xsd file, it say it is there, but in the design it is not and I'm unsure of what to do to get it to show there. Here is my code:
private void QBC_CM_Form_Load(object sender, EventArgs e)
{
// fill the form with the users on form load
this.usersTableAdapter.Fill(this.qbc_clothing_ministryDS.users);
}
private void radioBtnUserReports_CheckedChanged(object sender, EventArgs e)
{
// since user reports was selected
// bind the datasource to this.usersBindingSource (bound to users table)
dgvReports.DataSource = this.usersBindingSource;
// rename the header text visible (so it's not the database column names)
dgvReports.Columns[0].HeaderText = "ID";
dgvReports.Columns[1].HeaderText = "First Name";
dgvReports.Columns[2].HeaderText = "Last Name";
dgvReports.Columns[3].HeaderText = "Address";
dgvReports.Columns[4].HeaderText = "Phone";
dgvReports.Columns[5].HeaderText = "Cell";
dgvReports.Columns[6].HeaderText = "Email";
}
private void radioBtnClothingReports_CheckedChanged(object sender, EventArgs e)
{
// since the clothing reports was selected
// bind the datasource to this.qbc_clothing_ministryDataSet1.clothing_reports (clothing reports table)
this.qbcClothingReportsBindingSource.DataSource = this.qbc_clothing_ministryDataSet1.clothing_reports;
dgvReports.DataSource = this.qbcClothingReportsBindingSource;
// rename the header text visible (so it's not the database column names)
dgvReports.Columns[0].HeaderText = "ID";
dgvReports.Columns[1].HeaderText = "Name";
dgvReports.Columns[2].HeaderText = "Clothing Taken";
dgvReports.Columns[3].HeaderText = "Month";
dgvReports.Columns[4].HeaderText = "Year";
// why is this data from the db not showing up?
}
Here are some screenshots that also will help explain (hopefully)
.xsd file:
form design file:
program running -
and finally the data in the database (mssql server) -
I know if I could reference the table adapter, I think I could load the data but it's not showing up (like usersTableAdapter).
Any help would be appreciated
Thanks!
Try going to the project tab in Visual Studio (VS) and click the Add New Data Source... option. A window will pop-up choose the Database option and click [Next>]. Then click Dataset and again click [Next>]. This will bring up the data connection settings. Create a New Connection by clicking the [New Connection...] button. This will allow you to add a connection. Enter your server name in the server name text box. You can then enter the details on login in the Log on to server section of this window. Once you do those things click the [Test Connection] button and you should be all good. If that does not work then make sure you entered all the correct info on this window.
If its all good click [Ok] on that window and then click [Next>], a window will check boxes should appear. Under tables selected all the ones you want to use. Then click [Finish].
Now you should be able to use the table adapters for the data tables in the database, for example:
var myTableAdapter = new nameYouGaveDataSetTableAdapters.youTableTableAdapter();
System.Data.DataTable dt = myTableAdapter.GetData();
this will load in the data to the data table dt."nameYouGaveDataSetTableAdapters" is default named as yourDataBaseTableAdapters.
Related
I'm using the table adapter in C# where I created a DataGridView. When I double click a row, I am displaying all the cells in the row in textboxes in a different Form. After I edit the textboxes and click my save button, I would like to take the values from the textboxes and replace them in the database. I can see the changes on the DataGrid, however I am not able to save them in the database.
private void InventoryData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//make new form and display them there
ViewInventoryItem ViewItem = new ViewInventoryItem();
ViewItem.textBox1.Text = this.InventoryData.CurrentRow.Cells[1].Value.ToString();
ViewItem.textBox2.Text = this.InventoryData.CurrentRow.Cells[2].Value.ToString();
ViewItem.textBox3.Text = this.InventoryData.CurrentRow.Cells[3].Value.ToString();
ViewItem.ShowDialog();
if (ViewItem.DialogResult == DialogResult.OK)
{
//save button was pressed
this.InventoryData.CurrentRow.Cells[0].Value = ViewItem.textBox1.Text;
this.InventoryData.CurrentRow.Cells[1].Value = ViewItem.textBox2.Text;
this.InventoryData.CurrentRow.Cells[2].Value = ViewItem.textBox3.Text;
this.Validate();
this.InventoryData.EndEdit();
this.booksTableAdapter.Update(this.InventoryDataSet.Books);
}
}
Found the problem. In the Dataset the Table had only Fill and Get Data. Even after selecting the Wizard to add the other statements it was failing. After adding the UpdateQuery manually, the code works without issues. Thank you Crowcoder for pointing me in the right direction.
I have two user controls. One is to input data and another one has a combobox that displays data from my MySQL table.
The use types in data in the first user control and presses a button. This adds the data to a MySQL table. I want to add the data immediately / automatically into the combobox (the other user control).
I would prefer not doing it using an event. If it is not possible and I have to use an event, what event should I use? Can it be an event not associated to the button?
Here is the method that reads data from MySQL and adds it to the combobox :
private void LoadFromDatabase()
{
string query = "select name from country";
MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand command = new MySqlCommand(query,conn);
MySqlDataReader Read;
conn.Open();
Read = command.ExecuteReader();
while(Read.Read())
{
metroComboBox1.Items.Add(Read.GetString("name"));
}
conn.Close();
}
The current result is that I must reload the windows form to load the new data into thecombobox. Without the reload, the combobox only displays the old data. I have put that method under InitializeComponent(); of the combobox user control.
You can use the event click on the comboBox itself, so whenever you click it to open the dropdown it will update the combobox. Make sure to clear the items at the top of the click event to prevent duplicates.
If you truly insist on having no events, you can use a background worker, however for what you're doing it seems like overkill to spawn a thread just for that.
This is an idea of what the final result should look like.
private void comboBox1_Click(object sender, EventArgs e)
{
if (comboBox1.Items.Count > 0)
comboBox1.Items.Clear();
LoadFromDatabase();
comboBox1.SelectedItem = 0;
comboBox1.Text = "Select Item";
}
So I'm trying to cope with an app project in c#/mssql.
It is Windows Froms app.
I'm connected to db, I can show all the tables in my db in the form onload, but i don't know how, or if I can do sth like: when these table names shows click on any and it will send the db name as a variable to function, which will show me the content of the folowing table. I've learned about cellclick event, but i still don't know how do i make it work.
So the code below works perfectly fine
DbClassShow showObj = new DbClassShow();
private void MyWindow_Load(object sender, EventArgs e)
{
DataTable dt = showObj.Select();
QueryView.DataSource = dt;
}
but I want it to show the content of the table, when i click on it, but I can't attach like clik event to table name, because when the app is not running i can't even see content of the data grid view.
What do I do in this situation?
Sorry guys for bothering, I've been trying this and had no clue, but finally I resolved it on my own! Here's what i did:
dbClassTables showTab = new dbClassTables();
private void QueryView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int indexOfRow = e.RowIndex;
DataGridViewRow selectedRow = QueryView.Rows[indexOfRow];
DataTable dt1 = showTab.Select(selectedRow.Cells[2].Value.ToString());
QueryView.DataSource = dt1;
}
Created a new class with select method with parameter and it works, I'm gonna make it more valid using abstract class or interface i think, but yeah, that's it.
public DataTable Select(string tbl_name)
{
...
string sql = "SELECT * FROM {0}";
string data = tbl_name;
sql= string.Format(sql, data);
My datagrid view consist with rows of data which are imported from a MySQL database. What I need to do is to load data of the selected row from the datagrid view to a separate form on a button click event.
This is the screenshot of my system
I have already implemented this by making the Update Book Title form text boxes modifiers to Public and writing this coding.
private void btn_update_Click(object sender, EventArgs e)
{
Update_Book_Title form = new Update_Book_Title();
form.txt_booknumber.Text = this.datagrid_booktitles.CurrentRow.Cells[1].Value.ToString();
form.txt_isbn.Text = this.datagrid_booktitles.CurrentRow.Cells[2].Value.ToString();
form.txt_author.Text = this.datagrid_booktitles.CurrentRow.Cells[3].Value.ToString();
form.txt_booktitle.Text = this.datagrid_booktitles.CurrentRow.Cells[4].Value.ToString();
form.txt_publishedyear.Text = this.datagrid_booktitles.CurrentRow.Cells[5].Value.ToString();
form.txt_publisher.Text = this.datagrid_booktitles.CurrentRow.Cells[6].Value.ToString();
form.txt_category.Text = this.datagrid_booktitles.CurrentRow.Cells[7].Value.ToString();
form.arrived_date.Text = this.datagrid_booktitles.CurrentRow.Cells[8].Value.ToString();
form.txt_price.Text = this.datagrid_booktitles.CurrentRow.Cells[9].Value.ToString();
form.txt_quantity.Text = this.datagrid_booktitles.CurrentRow.Cells[10].Value.ToString();
form.StartPosition = FormStartPosition.CenterScreen;
form.MdiParent = this.MdiParent;
form.Show();
}
I know this is not the best solution for this, Are there any alternatives in doing this, So that I can load data from my datagrid view to Update Book Title windows form on Update button click event?
Use property (POCO) class to assign the values from DataGridView and load them into entry form.
I have a windows forms application containing a datagridview control. The datagridview is populated by the contents of an xml file. At the moment, all of the columns are displayed as datagridviewtextboxcolumns. I want to select one that is populated by a particular xml tag and display it's content in a datagridviewcomboboxcolumn along with 2 other options.
EXAMPLE:
<SMS>
<Number>+447931663542</Number>
<DateTime>2009-07-12T17:00:02</DateTime>
<Message>YES</Message>
<FollowedUpBy>Unassigned</FollowedUpBy>
<Outcome>Resolved</Outcome>
</SMS>
The OUTCOME tag is the column that I would like to be displayed as a comboboxcolumn in the datagridview. If for example the tag is empty and contains no data, then I want to display nothing, but have the comboboxcolumn populated with 3 possible options to choose from (Unresolved, Resolved, Pending). If however the tag contains data, I want that particular item to be displayed in the comboboxcolumn, and have the other two options available to be selected.
Help in achieving this would be appreciated greatly!
Regards,
EDIT:
Currently I use this code:
colOutcome = new DataGridViewComboBoxColumn();
colOutcome.HeaderText = "Outcome";
colOutcome.Width = 90;
colOutcome.Items.AddRange("Resolved", "Unresolved", "Pending");
this.dataGridView1.Columns.Insert(1, colOutcome);
this.dataGridView1.Columns[1].Name = "OutcomeColumn";
This code above populates the combobox. THE PROBLEM IS: When The xml document populates the datagridview, the outcome column just appears as a textbox column, containing the data inbetween the outcome tags in the xml file. My point is, how can i get the datagridview to realise when it reads the outcome column that it needs to be changed into a combobox column and then display the data that way, along with the other potentially selectable options in the combobox?! Currently the datagridview gets populated with all columns as textboxcolumns containing the data, as well as a seperate combobox column which is not what I want. I need the application to merge the outcome column and its data with the code above.
Any ideas?
Updated Answer
You could pass in the XML document to a function that will loop through each node and determine whether it should be a ComboBox one or not i.e. if the name is "Outcome".
private void CreateColumns(XmlDocument doc)
{
foreach (...) // loop through each node in xml document
{
if (node.Name == "Outcome")
{
var items = new List<string>() { "Resolved", "Unresolved", "Pending" };
this.dataGridView1.Columns.Add(CreateComboBoxColumn(node.Name, items));
}
else
{
this.dataGridView1.Columns.Add(String.Format("col{0}", node.Name), node.Name);
}
}
}
Then your code for creating the Outcome column would be:
private DataGridViewComboBoxColumn CreateComboBoxColumn(string colHeaderText, List<string> items)
{
var colOutcome = new DataGridViewComboBoxColumn();
colOutcome.HeaderText = colHeaderText;
colOutcome.Width = 90;
colOutcome.Items.AddRange(items.ToArray());
colOutcome.Name = String.Format("col{0}", colHeaderText);
return colOutcome;
}
You would then just call CreateColumns on the form load event and pass in your XML. You should only need to create the columns once.
My advice would be to have a similar function that will find all the SMS elements and add a new row populating it with the information in each node.
public void MyForm_Load(object sender, EventArgs e)
{
var doc = new XmlDocument(filename);
CreateColumns(doc);
CreateRows(doc);
}
Hope that helps.
Answer #2 for me, based on the updated question.
The problem you are experiencing is with the AutoGeneratedColumns functionality of the DataGridView. You will need to create your columns manually before databinding. This can be done at design-time or run-time. I prefer design-time because it gives you a bit more direction with the look/feel of the grid but either way works.
You will need to disable the AutoGeneratedColumns property of the grid:
private void Form1_Load(object sender, EventArgs e)
{
// Define your columns at run-time here if that's what you prefer
this.dataGridView1.AutoGeneratedColumns = false;
this.dataGridView1.DataSource = myDataSource;
}
I'm not sitting in front of VS so this might not compile but should give you direction.
You need to either pre-populate the ResolvedColumn with the 3-4 possible values at design-time or assign it to another datasource at runtime. If you chose the design-time approach, simply open the DataGridView "Edit Columns" dialog, find the ResolvedColumn, go to Items, and add your values ("", "Unresolved", "Pending", "Resolved"). The empty value might help the ComboBox to render if there is the possiblity of rendering the grid with SMS records that have no Outcome.
To bind the possible options at runtime do something like this:
private List<string> _outcomeDataSource;
private void Form1_Load(object sender, EventArgs e)
{
_outcomeDataSource = new List<string>;
_outcomeDataSource.Add("");
_outcomeDataSource.Add("Unresolved");
_outcomeDataSource.Add("Pending");
_outcomeDataSource.Add("Resolved");
ResolvedColumn.DataSource = _outcomeDataSource;
ResolvedColumn.PropertyName = "Outcome";
}