Show combobox item dynamically, depending on the record - c#

I want to display a Comboxbox Item, depending on a data table.
for example:
1 = open
2 = close
....
The combobox content is created with the datatable:
string sqlStr = "select id, description from sdg.pa_status";
MySqlDataAdapter adapt = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand();
DataSet ds = new DataSet();
cmd = new MySqlCommand(sqlStr, conn);
adapt.SelectCommand = cmd;
adapt.Fill(ds);
**_status** = ds.Tables[0];
combox is created:
column.Name = "status";
column.DropDownWidth = 160;
column.Width = 160;
column.FlatStyle = 0;
column.HeaderText = "Status";
column.DataSource = **_status**;
column.DataPropertyName = dGVQuote.Columns["**stat**"].ToString();
column.ValueMember = _status.Columns[1].ToString();
column.DisplayMember = _status.Columns[1].ToString();
datagrid is generated:
private void CustomizeDataGridViewOrder()
{
dGVQuote.DataSource = GetQuote("");
dGVQuote.Columns["ID"].Width = 135;
dGVQuote.Columns["ID"].HeaderText = "Angebotsnummer";
dGVQuote.Columns["idorder"].Visible = false;
dGVQuote.Columns["Description"].Width = 225;
dGVQuote.Columns["Description"].HeaderText = "Beschreibung";
dGVQuote.Columns["comment"].Width = 225;
dGVQuote.Columns["comment"].HeaderText = "interner Kommentar";
dGVQuote.Columns["idcust"].Visible = false;
dGVQuote.Columns["idobj"].Visible = false;
dGVQuote.Columns["cnt"].Width = 60;
dGVQuote.Columns["cnt"].HeaderText = "Anzahl MA";
dGVQuote.Columns["**stat**"].Visible = true;
dGVQuote.Columns.Add(CreateComboBoxColumn());
dGVQuote.Columns["valid"].Width = 135;
dGVQuote.Columns["valid"].HeaderText = "gültig bis";
dGVQuote.DataSource = GetQuote(""); //datatable is handed over
I am desperate. Can someone help me?

Related

datagridview column name C#

i am trying to set custom name for my column names in c# but when i load the form i dont get the names here is an example of my code:
string Konekcija = "datasource=localhost;port=3309;username=root;password=1234";
string Query = "select * from baza.artikli";
MySqlConnection konn = new MySqlConnection(Konekcija);
MySqlCommand komanda = new MySqlCommand(Query, konn);
MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
MyAdapter.SelectCommand = komanda;
DataTable dTable = new DataTable();
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
dataGridView1.ColumnHeadersVisible = true;
// имиња на header ите
dataGridView1.AutoResizeColumnHeadersHeight();
dataGridView1.Columns[0].Name = "ид";
dataGridView1.Columns[1].Name = "Баркод";
dataGridView1.Columns[2].Name = "Назив";
dataGridView1.Columns[3].Name = "Набавна цена";
dataGridView1.Columns[4].Name = "Продажна цена";
dataGridView1.Columns[5].Name = "Данок";
dataGridView1.Columns[6].Name = "Количина";
dataGridView1.Columns[7].Name = "Опис";
dataGridView1.Columns[8].Name = "Македонски производ";
dataGridView1.Columns[9].Name = "Производител";
}
Maybe i am missing something but i cant seem to find what it is so if you now please le me know
Try this,
dataGridView1.Columns[index].HeaderText= "something";
Hope helps,

Select Data SQL to DataGridView

I tried bind the DataGridView from Table "NatureCharge"
private void BindGrid()
{
DataGridViewNatureCharge.DataSource = null;
using (SqlConnection con = new SqlConnection(connstring))
{
using (SqlCommand cmd = new SqlCommand("select * from NatureCharge", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
DataGridViewNatureCharge.DataSource = dt;
}
}
}
}
}
But I need to show Nom Famille not the Id, so what is the modification on select query???
"select * from NatureCharge where Idfam =(select NomFam from Famille)"
Update
the problem with NomFam created in another cell in DatagridView.
I need to add it in 3rd cell.
select n.IdNat,n.NomNat,f.NomFam from NatureCharge n join Famille f on n.IdFam=f.IdFam
The DataGridView
//Set Columns Count
DataGridViewNatureCharge.ColumnCount = 3;
//Hide the last blank line
DataGridViewNatureCharge.AllowUserToAddRows = false;
//Add Columns
DataGridViewNatureCharge.Columns[0].Name = "IdNat";
DataGridViewNatureCharge.Columns[0].HeaderText = "N° Nature de Charge";
DataGridViewNatureCharge.Columns[0].DataPropertyName = "IdNat";
DataGridViewNatureCharge.Columns[0].Width = 100;
DataGridViewNatureCharge.Columns[1].HeaderText = "Nom de Nature de Charge";
DataGridViewNatureCharge.Columns[1].Name = "NomNat";
DataGridViewNatureCharge.Columns[1].DataPropertyName = "NomNat";
DataGridViewNatureCharge.Columns[1].Width = 150;
DataGridViewNatureCharge.Columns[2].Name = "IdFam";
DataGridViewNatureCharge.Columns[2].HeaderText = "Nom de Famille";
DataGridViewNatureCharge.Columns[2].DataPropertyName = "IdFam";
DataGridViewNatureCharge.Columns[2].Width = 100;
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
DataGridViewNatureCharge.Columns.Insert(0, checkBoxColumn);
Use SQL joins for this.
SELECT A.NomFam, B.IdNat, B.NomNat FROM Famille A join NatureChange B on A.IdFam = B.IdFam

Creating column Combobox in Datagridview

I am new to C#. I want to create combobox in first column of my Datagridview. Following is the routine I have wrote. But it is adding combo in last column after setting up my grid.
For setting up Grid, i have tried the below code:
private void SetGrid()
{
dgDetail.AutoGenerateColumns = false;
dgDetail.ColumnCount = 5;
dgDetail.Columns[0].Name = "Debit";
dgDetail.Columns[0].HeaderText = "Debit Account Name";
dgDetail.Columns[1].Name = "Bank";
dgDetail.Columns[1].HeaderText = "Bank";
dgDetail.Columns[2].Name = "ChqNo";
dgDetail.Columns[2].HeaderText = "CC/Chq No";
dgDetail.Columns[3].Name = "ChqDate";
dgDetail.Columns[3].HeaderText = "Chq Date";
dgDetail.Columns[4].Name = "Amount";
dgDetail.Columns[4].HeaderText = "Amount";
dgDetail.AllowUserToDeleteRows = true;
dgDetail.Columns[0].Width = 280;
dgDetail.Columns[1].Width = 160;
dgDetail.Columns[2].Width = 90;
dgDetail.Columns[3].Width = 90;
dgDetail.Columns[4].Width = 120;
dgDetail.RowsDefaultCellStyle.ForeColor = Color.Black;
dgDetail.RowsDefaultCellStyle.BackColor = Color.White;
dgDetail.Font = new Font("Arial", 9, FontStyle.Regular);
}
For Creating the combobox which is filled from DB.
private void FillGridCombo()
{
SqlConnection sqlConnection = new SqlConnection(strCon);
sqlConnection.Open();
try
{
string selectQueryStringMonth = "SELECT accode, GLAC FROM glmast where (actype = 'CSH' and titleac <> 'PDP') OR TITLEAC = 'DIS' ORDER BY GLAC";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
BindingSource bindingSourceMonth = new BindingSource();
bindingSource.DataSource = dataTable;
//Adding Combo
DataGridViewComboBoxColumn ColumnAcc = new DataGridViewComboBoxColumn();
ColumnAcc.DataPropertyName = "Debit Account Name";
ColumnAcc.HeaderText = "Debit Account Name";
ColumnAcc.Width = 280;
ColumnAcc.DataSource = bindingSourceMonth;
ColumnAcc.ValueMember = "accode";
ColumnAcc.DisplayMember = "GLAC";
dgDetail.Columns.Add(ColumnAcc);
dgDetail.DataSource = bindingSource;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (sqlConnection.State != ConnectionState.Closed)
sqlConnection.Close();
}
}
I am calling both procedures on my NewData() like this.
private void NewData()
{
if (dgDetail.DataSource != null)
dgDetail.DataSource = null;
else
dgDetail.Rows.Clear();
ClearData();
CtrlEnable();
SetGrid();
FillGridCombo();
}
Help / Guide me to achieve this.,
use
dgDetail.Columns.Insert(0, ColumnAcc);
instead of
dgDetail.Columns.Add(ColumnAcc);
when you use Add it simply adds it as the last column, while using Insert you can choose where to add it to.
public virtual void Insert( int columnIndex, DataGridViewColumn
dataGridViewColumn )
note that the columnIndex is a zero-based index so 0 is the first column
You can use the Insert method instead of Add like this:
dgDetail.Columns.Insert(0,ColumnAcc);

Multiple gridview Rows Insert in Database at time

In my below code give me this error
error is my gridview textbox column value not inserting in
my database parameter missing error
try {
da=DateTime.Now;
foreach(DataGridViewRow row in dataGridView1.Rows) {
string SelectedText=Convert.ToString((row.Cells["article_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
string SelectedText1=Convert.ToString((row.Cells["size_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
string SelectedText2=Convert.ToString((row.Cells["color_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
string SelectedText3=Convert.ToString((row.Cells["unit_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
SqlCommand comm=new SqlCommand("abc", con);
con.Open();
comm.Parameters.Add("#order_no", SqlDbType.NVarChar).Value="abc123";
comm.Parameters.Add("#article_name", SqlDbType.NVarChar).Value=SelectedText;
//dataGridView1.Rows[i].Cells["article_name"].Value;
comm.Parameters.Add("#size_name", SqlDbType.NVarChar).Value=SelectedText1;
comm.Parameters.Add("#color_name", SqlDbType.NVarChar).Value=SelectedText2;
comm.Parameters.Add("#quantity", SqlDbType.Int).Value=row.Cells["quantity"].Value;
comm.Parameters.Add("#piece_carton", SqlDbType.Int).Value=row.Cells["pcs_carton"].Value;
comm.Parameters.Add("#no_of_carton", SqlDbType.Int).Value=row.Cells["no_carton"].Value;
comm.Parameters.Add("#unit_name", SqlDbType.NVarChar).Value=SelectedText3;
comm.Parameters.Add("#rate", SqlDbType.NVarChar).Value=row.Cells["rate"].Value;
comm.Parameters.Add("#ship_qty", SqlDbType.Int).Value=row.Cells["ship_qty"].Value;
comm.Parameters.Add("#date1", SqlDbType.DateTime).Value=da;
comm.Parameters.Add("#amount", SqlDbType.NVarChar).Value=row.Cells["amount"].Value;
//comm.CommandText = StrQuery;
db.insertprocedure(comm);
}
}
catch(SqlException ex) {
MessageBox.Show(ex.Message);
}
finally {
con.Close();
}
Here Is My Query
create proc abc
#order_no varchar(50),
#article_name varchar(500),
#size_name varchar(100),
#color_name varchar(100),
#quantity int,
#piece_carton int,
#no_of_carton int,
#unit_name varchar(50),
#rate varchar(50),
#ship_qty int,
#date1 date,
#amount varchar(50)
as
begin
set #date1=CONVERT(varchar,#date1,1)
insert into transaction_order(order_id,article_id,size_id,color_id,quantity,piece_carton,no_of_carton,unit_id,rate,ship_qty,date1,amount)
values(#order_no,(select article_id from article_order where article_name=#article_name),(select size_id from size where size_name=#size_name),(select color_id from color where color_name=#color_name),#quantity,#piece_carton,#no_of_carton,(select unit_id from unit where unit_name=#unit_name),#rate,#ship_qty,#date1,#amount)
end
Here Is MY Function insertprocedure this function is in database class
public void insertprocedure(SqlCommand txt)
{
DateTime da = new DateTime();
try
{
da = DateTime.Now;
con.Open();
txt.CommandType = CommandType.StoredProcedure;
txt.ExecuteNonQuery();
con.Close();
}
catch (Exception ex) { throw ex; }
finally { con.Close(); }
}
> Here Is My Gridview TextBox Columns Added Code
private void Form1_Load(object sender, EventArgs e)
{
string query = "SELECT article_name FROM article_order";
DataTable dt = db.select_command(query);
BindingSource bi = new BindingSource();
bi.DataSource = dt;
string query1 = "SELECT size_name FROM size";
DataTable dt1 = db.select_command(query1);
BindingSource bi1 = new BindingSource();
bi1.DataSource = dt1;
string query2 = "SELECT color_name FROM color";
DataTable dt2 = db.select_command(query2);
BindingSource bi2 = new BindingSource();
bi2.DataSource = dt2;
string query3 = "SELECT unit_name FROM unit";
DataTable dt3 = db.select_command(query3);
BindingSource bi3 = new BindingSource();
bi3.DataSource = dt3;
//string query4 = "SELECT order_no FROM master_order";
//DataTable dt4 = db.select_command(query4);
//BindingSource bi4 = new BindingSource();
//bi4.DataSource = dt4;
#region Gridview Columns
//Transaction Id
DataGridViewTextBoxColumn trans_id = new DataGridViewTextBoxColumn();
trans_id.HeaderText = "ID";
trans_id.Width = 50;
trans_id.DataPropertyName = "ID";
trans_id.Name = "trans_id";
dataGridView1.Columns.Add(trans_id);
//Order No.
//DataGridViewTextBoxColumn order_no = new DataGridViewTextBoxColumn();
//order_no.HeaderText = "Order No";
//order_no.Width = 100;
////order_no.DataPropertyName = "yarn_name";
//order_no.Name = "order_no";
//dataGridView1.Columns.Add(order_no);
//query = "select yarn_name from yarn";
//auto.autocomplete1(dataGridView1., textBox9.AutoCompleteCustomSource = namesCollection, query);
//DataGridViewTextBoxColumn order_no = new DataGridViewTextBoxColumn();
//order_no.HeaderText = "Order No.";
//order_no.Width = 50;
//order_no.DataPropertyName = "order_no";
//order_no.Name = "order_no";
//dataGridView1.Columns.Add(order_no);
//Article Name
DataGridViewComboBoxColumn article_name = new DataGridViewComboBoxColumn();
article_name.DataPropertyName = "Article Name";
article_name.HeaderText = "Article Name";
article_name.Name = "article_name";
article_name.Width = 120;
article_name.DataSource = bi;
article_name.ValueMember = "article_name";
//article_name.DisplayMember = "article_name";
dataGridView1.Columns.Add(article_name);
//Size Name
DataGridViewComboBoxColumn size_name = new DataGridViewComboBoxColumn();
size_name.DataPropertyName = "Size Name";
size_name.HeaderText = "Size Name";
size_name.Name = "size_name";
size_name.Width = 120;
size_name.DataSource = bi1;
size_name.ValueMember = "size_name";
////size_name1.DisplayMember = "size_name";
dataGridView1.Columns.Add(size_name);
//Color Name
DataGridViewComboBoxColumn color_name = new DataGridViewComboBoxColumn();
color_name.DataPropertyName = "Color Name";
color_name.HeaderText = "Color Name";
color_name.Name = "color_name";
color_name.Width = 120;
color_name.DataSource = bi2;
color_name.ValueMember = "color_name";
//ColumnItem.DisplayMember = "ItemText";
dataGridView1.Columns.Add(color_name);
//quantity
DataGridViewTextBoxColumn quantity = new DataGridViewTextBoxColumn();
quantity.HeaderText = "Quantity";
quantity.Width = 100;
quantity.DataPropertyName = "Quantity";
quantity.Name = "quantity";
dataGridView1.Columns.Add(quantity);
//Pcs/Carton
DataGridViewTextBoxColumn pcs_carton = new DataGridViewTextBoxColumn();
pcs_carton.HeaderText = "Piece/Carton";
pcs_carton.Width = 100;
pcs_carton.DataPropertyName = "Piece/Carton";
pcs_carton.Name = "pcs_carton";
dataGridView1.Columns.Add(pcs_carton);
//No Of Carton
DataGridViewTextBoxColumn no_carton = new DataGridViewTextBoxColumn();
no_carton.HeaderText = "No Of Carton";
no_carton.Width = 70;
no_carton.DataPropertyName = "No Of Carton";
no_carton.Name = "no_carton";
dataGridView1.Columns.Add(no_carton);
//unit name
DataGridViewComboBoxColumn unit_name = new DataGridViewComboBoxColumn();
unit_name.DataPropertyName = "Unit Name";
unit_name.HeaderText = "Unit Name";
unit_name.Name = "unit_name";
unit_name.Width = 120;
unit_name.DataSource = bi3;
unit_name.ValueMember = "unit_name";
//ColumnItem.DisplayMember = "ItemText";
dataGridView1.Columns.Add(unit_name);
//rate
DataGridViewTextBoxColumn rate = new DataGridViewTextBoxColumn();
rate.HeaderText = "Rate";
rate.Width = 70;
rate.DataPropertyName = "Rate";
rate.Name = "rate";
dataGridView1.Columns.Add(rate);
//amount
DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
amount.HeaderText = "Amount";
amount.Width = 70;
amount.DataPropertyName = "Amount";
amount.Name = "amount";
dataGridView1.Columns.Add(amount);
//Ship quantity
DataGridViewTextBoxColumn ship_qty = new DataGridViewTextBoxColumn();
ship_qty.HeaderText = "Ship Quantity";
ship_qty.Width = 70;
ship_qty.DataPropertyName = "Ship Quantity";
ship_qty.Name = "ship_qty";
dataGridView1.Columns.Add(ship_qty);
#endregion
}
values(#order_no,
(select article_id from article_order where article_name=#article_name),
(select size_id from size where size_name=#size_name),
(select color_id from color where color_name=#color_name),
#quantity,#piece_carton,#no_of_carton,
(select unit_id from unit where unit_name=#unit_name),
#rate,#ship_qty,#date1,#amount)
The error message would show up if any of the marked subselects in your procedure returns more than one value.
Can we see your select query? it seems that there is a subquery that returns more then 1 value
Run this query:
select article_name,COUNT(*) from article_order GROUP BY article_name HAVING COUNT(*)>1
If it returns ANY rows, then your problem is that you have multiple articles in your table with the same name.
Alternatively, make sure that you have a UNIQUE INDEX on each of article_name, color_name, unit_name, and size_name. If you get an error while trying to create that index (because one will definitely give you an error), then you need to fix that in the appropriate table, then create the index.

Data in data grid view won't show visually C# SQL

I have this data grid view like this:
But it should be like this:
The data is successfully fetched but I have to drag the cells so the data can be shown.
This is the code:
private void button5_Click_1(object sender, EventArgs e)
{
sqlConnStaff();
groupBoxRoom.Visible = false;
groupBoxDPenghuni.Visible = false;
groupBoxPenghasilan.Visible = false;
groupBoxPenghuni.Visible = false;
groupBoxStaff.Visible = true;
GroupBox_AddResident_Resident.Visible = false;
GroupBox_AddResident_Room.Visible = false;
GroupBox_AddResident1.Visible = false;
GroupBox_DeleteResident_Resident.Visible = false;
GroupBox_DeleteResident1.Visible = false;
GroupBox_Resident.Visible = false;
GroupBox_Update_Room.Visible = false;
GroupBox_UpdateResident1.Visible = false;
}
private void sqlConnStaff()
{
BindingSource dbBindSource = new BindingSource();
SqlCommand com;
com = new SqlCommand();
SqlConnection con = new SqlConnection(strCon);
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "view_staff";
SqlDataAdapter dataAdapter = new SqlDataAdapter(com);
IDCabang = new SqlParameter();
IDCabang.SqlDbType = SqlDbType.VarChar;
IDCabang.Size = 5;
IDCabang.ParameterName = "#IDCabang";
IDCabang.Value = IDCabangC;
IDCabang.Direction = ParameterDirection.Input;
com.Parameters.Add(IDCabang);
con.Open();
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dbBindSource.DataSource = table;
dataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
// you can make it grid readonly.
dataGridView3.ReadOnly = true;
// finally bind the data to the grid
dataGridView3.DataSource = dbBindSource;
con.Close();
}
Whats wrong and what should I do?
It looks like problem is not with your c# code but the coloring scheme. When you select cells the data is visible, which means you must change fore color to something other than white.

Categories

Resources