c# show 2 columns of database with 28 columns - c#

I have a database with 28 columns. First column is code, second Name and the rest are values.
public void displayData()
{
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM gehaltes", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.AutoResizeColumn(DataGridViewAutoSizeColumnsMode.DisplayedCells);
con.Close();
}
With this part of the program I see all the columns in the datagridview. I only want the first 2 but I have to use all the 28 columns if I want to make changes.

You can set the visible property of individual columns:
DataGridView dgv;
// ...
foreach(DataGridViewColumn c in dgv.Columns)
c.Visible = c.Name == "code" || c.Name == "Name";

Set the AutoGenerateColumns property of the DataGridView to false, and add two columns with the property DataPropertyName refering to the names that you want to display.
Example:
da.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
DataGridViewColumn col = new DataGridViewColumn();
col.Name = "name";
col.HeaderText = "header";
col.DataPropertyName = "yourDBField";
dataGridView1.Columns.Add(col);
dataGridView1.DataSource = dt;
dataGridView1.AutoResizeColumn(DataGridViewAutoSizeColumnsMode.DisplayedCells);

Related

How to fill Datagridview in C# from DataTable adding custom columns?

I use the following code to bind DataTable with DataGridView:
DataTable dt = new DataTable();
dt = serviceSqlite.select(new Pacients());
dataGridView1.DataSource = dt;
I want to add custom columns in dt it should be one CheckBox and Image.
How to do that?
You can add them to your DataGridView. Like this:
var checkBox = new DataGridViewCheckBoxColumn
{
Name = "checkBox",
HeaderText = #"checkBox",
Width = 70
};
//Set other properties...
dataGridView1.Columns.Add(checkBox);
And same for Image:
DataGridViewImageColumn image = new DataGridViewImageColumn();
//....
Also if you want the new column to be appear in another column you can replace dataGridView1.Columns.Add(checkBox); with following:
dataGridView1.DataSource = dt;
var checkBox = new DataGridViewCheckBoxColumn
{
Name = "checkBox",
HeaderText = #"checkBox",
Width = 70
};
//Set other properties...
dataGridView1.Columns.Insert(1, checkBox);
And if you want to add it to your DataTable you can add it as boolean field in the DataTable:
dt.Columns.Add(new DataColumn("checkBox", typeof(bool)));
dataGridView1.DataSource = dt;
So when you bind the DataTable to your DataGridView, a CheckBoxColumn is created for this boolean field.

How can I add extra column in datagridview when I fetch data from database

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();
}

Modifying a column in DGV to be a Combobox Bound to DataBase

I created a view named "FamilyView" to join 2 tables(Families and SStatus) and replace an id in the first table(Families) by its name existing in the second table(SStatus) (the column is now called "Status")
This view is now displayed in a datagridview that the user can modify
Sda = new SqlDataAdapter("Select * from FamilyView",con);
Sda.Fill(ds);
dg.DataSource = ds.Tables[0];
dg.Refresh();
What i want is to transform the column cells "Status" to comboboxes containing all Names from SStatus
SqlDataAdapter sadapter = new SqlDataAdapter("Select * From SStatus", con);
DataSet ds1 = new DataSet();
sadapter.Fill(ds1);
i found this code:
DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(dg.Rows[i].Cells[0]);
ComboColumn.DataSource = ds1.Tables[0];
ComboColumn.DisplayMember = "Name";
but i can't replace the cell
and this code but I'm not sure how to use it:
Adding bound combobox to datagridview
BindingSource StatusBD = new BindingSource();
StatusBD.DataSource = ds1;
DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
colType.HeaderText = "Status";
colType.DropDownWidth = 90;
colType.Width = 90;
//colType.DataPropertyName = "Name";
colType.DataSource = ds;
colType.DisplayMember = "Name";
colType.ValueMember = "IdStatus";
dg.Columns.Insert(dg.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType);
Your data source is empty: it should contain 2 tables: one with the SStatus and one with Families.
and then follow my as in your post:
DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
BindingSource wizardBindingSource = new BindingSource();
wizardBindingSource.DataSource = dataSet;
wizardBindingSource.DataMember = "protocol"; // This is the table in the set
colType.HeaderText = "Type";
colType.DropDownWidth = 90;
colType.Width = 90;
colType.DataPropertyName = "wizardProtocol";
colType.DataSource = wizardBindingSource;
colType.DisplayMember = "protocolName";
colType.ValueMember = "idprotocols"
Here idProtocol (which is referenced as protocol in the "pcap" table) is mapped to protocolName.

Filling of ComboBox with Dataset

When am trying to fill Combobox
con.Open();
da = new SqlDataAdapter("Mt_Post_select",con);
//Mt_Post is Stored procedure with Select Command
ds1 = new DataSet();
da.Fill(ds1, "Mt_Post");
// The table has only one row
comboBox1.DataSource = ds1.Tables[0];
comboBox1.DisplayMember = "Mt_Post";
It bind But shows instead of data it having System.Data.DataRowView
What is the wrong this code Please anyone tell me
Replace Mt_Post in comboBox1.DisplayMember = "Mt_Post"; with the name of column whose value you would like to display.
I hope the following sample helps;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Email");
dt.AcceptChanges();
DataRow dr0 = dt.NewRow();
dr0[0] = "Kadir Sumerkent";
dr0[1] = "kadir#sumerkent.com";
DataRow dr1 = dt.NewRow();
dr1[0] = "Kadir Sumerkent 2";
dr1[1] = "kadir#sumerkent2.com";
dt.Rows.Add(dr0);
dt.Rows.Add(dr1);
dt.AcceptChanges();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Email";
comboBox1.DataSource = dt;
Let's try this:
Suppose your Stored Procedure is like the code below:
SELECT
Users.User_name AS username
Users.User_Code AS userID
FROM Users
-- WHERE CONDITONS ARE APPLIED
Now in order to Fetch this DataSet you are going to do as follows:
// YOUR OWN CODE
con.Open();
da = new SqlDataAdapter("Mt_Post_select",con);
//Mt_Post is Stored procedure with Select Command
ds1 = new DataSet();
// JUST FILL THE DataSet in THIS WAY
da.Fill(ds1);
// JUST TO MAKE SURE WE HAVE AN EMPTY COMBOBOX
comboBox1.Items.Clear();
comboBox1.DataSource = null;
// NOW POPULATE comboBox1 LIKE THIS
if (ds.Tables[0].Rows.Count > 0)
{
comboBox1.DataSource = ds1.Tables[0];
comboBox1.DisplayMember = "username";
comboBox1.ValueMember = "userID"
}

Duplicate column in DataTable when adding row

I create a DataTable and bind it to a DataGrid. My DataSource consist of one Table (FooTable) which consist of one column (FooName).
The following codes runs fine - except that each time I add a new row, there is a duplicate Column fill in with the same data, which I don't know how to get rid of. See below image and code. I have only one FooName column and a duplicate column comes out.
/* Create a DataGrid dg1 */
DataGrid dg1 = new DataGrid();
DataGridTextColumn col = new DataGridTextColumn();
col = new DataGridTextColumn();
colA.Binding = new Binding("FooName");
colA.Header = "FooName";
dg1.Columns.Add(colA);
dataGrid1.Children.Add(dg1);
/* Create a DataTable and bind it to DataGrid */
SqlCeDataAdapter da = new SqlCeDataAdapter();
string sqlStr = #"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(sqlStr, conn);
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Donkey";
dt.Rows.Add(newRow);
dg1.ItemsSource = ds.Tables[0].DefaultView;
try setting AutoGenerateColumns to false
dg1.AutoGenerateColumns = false
Try
dg1.AutoGenerateColumns = false;
Should do the work for you. For now the datagrid automatically generates the columns AND adds the one you asked

Categories