Winform has 2 grids. I display related tables from the SQL Server database to them.
Here is the code:
string connectionString = ConfigurationManager.ConnectionStrings["connectionSIPiT"].ConnectionString;
string command = "SELECT UserID, UserName,Login, idArm FROM Users";
string command2 = "SELECT id, name FROM arm";
sqlConnection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(command2, sqlConnection);
SqlDataAdapter adapter1 = new SqlDataAdapter(command, sqlConnection);
DataSet dataset1 = new DataSet();
adapter.Fill(dataset1, "arm");
adapter1.Fill(dataset1, "Users");
DataColumn keyColumn = dataset1.Tables[0].Columns[0];
DataColumn foreignKeyColumn = dataset1.Tables[1].Columns[3];
dataset1.Relations.Add("armUsers", keyColumn, foreignKeyColumn);
bindingSource1.DataSource = dataset1;
bindingSource1.DataMember = "arm";
bindingSource2.DataSource = bindingSource1;
bindingSource2.DataMember = "armUsers";
gridControl1.DataSource = bindingSource1;
gridControl2.DataSource = bindingSource2;
Please help me figure out how to hide unnecessary columns in the GridControl. Such as Id) Can the DataTable be used? If possible an example
After you set the DataSource for both grids, you can set the Visible property for the Id column to false.
Assuming the DefaultView is a GridView, the following code should do the job.
(gridControl1.DefaultView as GridView).Columns["Id"].Visible = false;
Related
I`m having a problem displaying some data on a datagrid view. I send data to MySQL on form2 and then its supposed to be displayed in a datagrid view in form1. The datagrid view is updating perfectly but it seems that the datatable is not getting populated with the full information. There are 2 rows of data that should be displayed. I get 2 empty rows in datagrid view.I checked the database and the information is there. Is it my query that isnt right?
What I need are all the rows that have temp_quote.quote_id in the quotes_idquotes column. Can you help me out? Here is the code:
public void RefreshGrid_parts()
{
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
MySqlConnection con = new MySqlConnection(conSettings.ToString());
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from shopmanager.parts where quotes_idquotes = '" + temp_quote.quote_id + "';",con);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 6;
dataGridView1.Columns[0].HeaderText = "# Assembly";
dataGridView1.Columns[0].DataPropertyName = "assemblies_assembly_id";
dataGridView1.Columns[1].HeaderText = "# Part";
dataGridView1.Columns[1].DataPropertyName = "part_number";
dataGridView1.Columns[2].HeaderText = "# Item";
dataGridView1.Columns[2].DataPropertyName = "items_items_id";
dataGridView1.Columns[3].HeaderText = "# Description";
dataGridView1.Columns[3].DataPropertyName = "part_description";
dataGridView1.Columns[4].HeaderText = "Drawing Revision";
dataGridView1.Columns[4].DataPropertyName = "drawing_rev";
dataGridView1.Columns[5].HeaderText = "Quantity";
dataGridView1.Columns[5].DataPropertyName = "quantity";
dataGridView1.DataSource = dt;
con.Close();
}
Here is your property example:
(yes its mssql but the logic is the same just change the types, I don't have the mysql lib at hand right know. i guess you'll figure it out.)
SqlClient.SqlCommand comand = new SqlClient.SqlCommand("Select * From ExampleTable Where Colum1 = $1");
comand.Parameters.Add(new SqlClient.SqlParameter("$1", "1234") { DbType = DbType.Int32 });
this will also parse the decimal string into the correct data type (int32)
Try to comment the lines that create the headers to test if your data will appear in the DataGrid. Also put the AutoGenerateColumns to true. If it does, check the DataPropertyName if it's exactly the names of the columns on your query.
public void RefreshGrid_parts()
{
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
MySqlConnection con = new MySqlConnection(conSettings.ToString());
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from shopmanager.parts where quotes_idquotes = '" + temp_quote.quote_id + "';",con);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.AutoGenerateColumns = true;
//dataGridView1.ColumnCount = 6;
//dataGridView1.Columns[0].HeaderText = "# Assembly";
//dataGridView1.Columns[0].DataPropertyName = "assemblies_assembly_id";
//dataGridView1.Columns[1].HeaderText = "# Part";
//dataGridView1.Columns[1].DataPropertyName = "part_number";
//dataGridView1.Columns[2].HeaderText = "# Item";
//dataGridView1.Columns[2].DataPropertyName = "items_items_id";
//dataGridView1.Columns[3].HeaderText = "# Description";
//dataGridView1.Columns[3].DataPropertyName = "part_description";
//dataGridView1.Columns[4].HeaderText = "Drawing Revision";
//dataGridView1.Columns[4].DataPropertyName = "drawing_rev";
//dataGridView1.Columns[5].HeaderText = "Quantity";
//dataGridView1.Columns[5].DataPropertyName = "quantity";
dataGridView1.DataSource = dt;
con.Close();
}
maybe take Rodrigos suggestion and just let the control generate the columns.
if you want to only this six columns just change the query.
public void RefreshGrid_parts()
{
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
MySqlConnection con = new MySqlConnection(conSettings.ToString());
con.Open();
MySqlCommand cmd = new MySqlCommand("select assemblies_assembly_id as '# Assembly', part_number as '# Part', items_items_id as '# Item', part_description as '# Description', drawing_rev as 'Drawing Revision', quantity as 'Quantity' from shopmanager.parts where quotes_idquotes = $1;",con);
cmd.Parameters.Add(new MySqlParameter("$1", temp_quote.quote_id) { DbType = DbType.Int32 });
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
con.Close();
}
I feel like an idiot. The code is good. The problem was that the text color in the datagrid view was white in a white box so I could not see it. Sorry to have bothered you guys with this. 24 hours spent on a non-existing problem! Wow!
I have a MySQL connection in C# that supposedly puts the table data into a DataGridView, but it has a problem and it makes the rows for every account in my users table in my db, but it doesn't show the actual data. Can anyone help?
My code:
public Form1()
{
InitializeComponent();
string connectionString = "Server=mysql.dunkycart.com; Database=dunkycart; Uid=dunkycart; Pwd=rooB-dnK-sqL;"; //Set your MySQL connection string here.
string query = "SELECT name, username, email FROM users"; // set query to fetch data "Select * from tabelname";
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
}
The issue:
Nevermind, I just found a fix. What I had to do is set the DataPropertyName for each column in the DataGridView to the corresponding column in the db table.
Because you are using DataSet instead of DataTable so you need to change this:
adapter.Fill(ds);
To this:
adapter.Fill(ds,"tbl");
Or you can use a DataTable instead of DataSet:
DataTable dt = new DataTable();
adapter.Fill(dt);
I am a newbie to c#.net winforms application. I have a data grid wch displays some data and out of which one of the column shud be made a dropdownlist or combobox. how do I use that in my code. please help.
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridViewComboBoxColumn combo = (DataGridViewComboBoxColumn)dataGridView1.Rows[e.RowIndex].Cells[3].OwningColumn;
sql = "select NAME FROM Suppliers";
BindingSource bsource = new BindingSource();
bsource.DataSource = obj.SqlDataTable(sql);
dataGridView1.DataSource = bsource;
combo.HeaderText = "Select Supplier";
}
I want to populate the 3rd column of the data grid with supplier name from the corresponding suppliers table.The data grid view is already populated with data from a join query and one of the field is Supplier(wch I mwant to convert to a dropdown or combo box). let me know if you need any further info for clarifications.
I Modified my code as follows:
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Suppliers";
//execute sql data adapter to get supplier values
DataTable dt = obj.SqlDataTable("select NAME from CUSTOMERS");
foreach (DataRow supplier in dt.DataSet.Tables[0].Rows)
{
combo.Items.Add(supplier[0]);
}
dataGridView1.Columns.Add(combo);
now am getting a null reference exception at " dt.DataSet.Tables[0].Rows"
. plz help. I am not sure if am missing something.
Try this code
string strcon = #"Data Source=kp;Initial Catalog=Name;Integrated Security=True;Pooling=False";
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
DataGridViewComboBoxColumn dgvCmb;
public Form2()
{
InitializeComponent();
grdcmd();
}
public void grdcmd()
{
con = new SqlConnection(strcon);
con.Open();
string qry = "Select * from Dbname";
da = new SqlDataAdapter(qry, strcon);
dt = new DataTable();
da.Fill(dt);
dgvCmb = new DataGridViewComboBoxColumn();
foreach (DataRow row in dt.Rows)
{
dgvCmb.Items.Add(row["Fname"].ToString());
}
dataGridView1.Columns.Add(dgvCmb);
}
In my database I have 4 column. I fetch this database value in a datagridview. But I want to add two columns in datagridview. So, I want to make a datagridview with 6 columns. Within this 6 columns 4 columns will filled by database value. How can I do this?
OleDbConnection con = new OleDbConnection("CONNECTION STRING");
con.Open();
DataTable dtusers = new DataTable();
OleDbCommand cmd = new OleDbCommand("Select Code,Description,Qnty,Rate from PurchaseTable'", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
dataGridView1.Columns[0].Name = "Code ";
dataGridView1.Columns[1].Name = "Description";
dataGridView1.Columns[2].Name = "Qnty";
dataGridView1.Columns[3].Name = "Rate";
con.Close();
Here are 4 columns. Code,Description,Qnty,Rate. I want to add two more columns in this datagridview. Amount and Narration. But Amount and Narration columns are not present in PurchaseTable.How can I do this?
If you want blank columns then create them and insert. If the data is in the database then add a join to your query to retrieve the data.
Adding blank columns
DataGridView dgv = new DataGridView();
dgv.DataSource = dtusers;
DataGridViewColumn amount = new DataGridViewColumn();
amount.HeaderText = "Amount";
amount.Name = "Amount";
dgv.Columns.Insert(0, amount);
DataGridViewColumn narration = new DataGridViewColumn();
narration.HeaderText = "Narration";
narration.Name = "Narration";
dgv.Columns.Insert(0, narration);
Assumed that you already have dtusers ..
Do Column adding to dtuser .. http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx
Do looping for all rows in your table to fill your new column
Assign the table as dgv datasource .. dataGridView1.DataSource = dtusers;
Because your column names are hardcoded, I think you can create a predefined columns in your datagridview through designer(Click datagridview -> choose Edit Columns).
Create all 6 columns you want. On the first four set DataPropertyName to name of the column from your query. Last two leave empty.
And remember set datagridview.AutoGeneratedColumns = false; before you binding data to datagridview...(somewhere in form_Load handler)
After this your code will be:
using (OleDbConnection con = new OleDbConnection("CONNECTION STRING"))
{
con.Open();
DataTable dtusers = new DataTable();
using(OleDbCommand cmd = new OleDbCommand("Select Code,Description,Qnty,Rate from PurchaseTable'", con))
{
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
}
con.Close();
}
I am doing some c# and mysql and I was successful at getting mysql data into a grid view for the first time! Now, my main question is, how do I manage the grid view style with this? For example, say I have already created columns and such, how do I put the mysql data into a specific column in the grid view?
Below is the code that is actually loading the data into the grid view.
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
// - DEBUG
// MessageBox.Show("Connection successful!");
MySqlDataAdapter MyDA = new MySqlDataAdapter();
MyDA.SelectCommand = new MySqlCommand("SELECT * FROM `swipes`", conn);
DataTable table = new DataTable();
MyDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dataGridView1.DataSource = bSource;
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
Close();
}
Also, this creates columns based on the mysql data, how do I modify the width of these columns and such, or like stated above, use my own custom columns for my data? I've never done any mysql work in any UI, so I'm open to suggestions and tutorials as well. Thanks in advance!
If you really want to do this (as someone has already stated you should look at other options) you can create the columns in the designer and set the DataGridViewColumn.DataPropertyName on each column to the columns returned by the autogenerated dataset. Remember to turn of autogeneration of columns (AutoGenerateColumns) on the grid. This way you have full control of the column styles.
try this
string connection = "server=localhost;database=adil;user=root;password=";
MySqlConnection con = new MySqlConnection(connection);
con.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = con;
MySqlDataAdapter MyDA = new MySqlDataAdapter();
string sqlSelectAll = "SELECT * from studentrec";
MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con);
DataTable table = new DataTable();
MyDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dataGridView1.DataSource = bSource;