How to add database columns from table to combobox - c#

private void Form1_Load(object sender, EventArgs e)
{
con.Open();
DataTable dt = con.GetSchema("TABLES").AsEnumerable().Where(x => x.Field<string>("TABLE_TYPE") == "TABLE").CopyToDataTable();
foreach (DataRow r in dt.Rows)
comboBoxTabel.Items.Add(r["TABLE_NAME"].ToString());
con.Close();
}
private void comboBoxTabel_SelectedIndexChanged(object sender, EventArgs e)
{
//con.Open();
//DataTable dt = con.GetSchema("COLUMNS");
//foreach (DataRow r in dt.Rows)
//comboBoxKolom.Items.Add(r.Field<string>("COLUMN_NAME"));
}
Can someone help me out please? When the program loads it shows all the tables from the database in the first combobox (comboBoxTabel). What i want is when you click on a table, that the columns of that table appear in the 2nd combobox (comboBoxKolom).

Related

how to filter a column in datagridview imported from excel file

For example I have a datagridview1 with data imported from a excel file and there are 12 columns: date, Name, Activity, Project,time, comment,ect. and 1000 row.
What I want to do is to filter only all with the Project name in project column.
for example I have support as a (Projectname) I want to show all columns filtyring by support rows.
I have combobox to select which column I need to filter it( e.g Project) here,
I tried with this code but it dose not work.
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string projektItem = comboBox1.Items[comboBox1.SelectedIndex].ToString();
if (projektItem == "Project") {
foreach (DataRow dataRow in dataGridView1.Rows)
{
StringBuilder filter = new StringBuilder();
for (int i = 0; i < dataGridView1.Columns.Count - 1; i++)
{
filter.Append(dataRow[i].ToString());
filter.Append("\t");
}
dataGridView1.DataSource = filter.ToString();
}
if (projektItem == "Name") {
}
if (projektItem == "Aktivity") {
}
}
This is how I do it. convert datagridview to datatable
And this is func for filter purpose:
Hold the origin table to go back if you turn your filter off
//datagrid to datatable
DataTable datatable = new DataTable();
datatable = (DataTable)dataGridView1.DataSource;
//datatableOrigin to hold your origin table
DataTable originTable = null;
// find Function
Public void Find(string column, string st)
{
DataRow[] dtResult;
DataTable holder = New DataTable;
//get datatable Schema
DataTable holder = datatable.Clone();
holder.Rows.Clear();
If (originTable != null)
datatable = originTable;
Else
originTable = datatable;
//select return datarow array
dtResult = datatable.Select("[" + column + "] LIKE '%" + st + "%'");
//import all your result into holder
foreach(DataRow dr In dtResult){holder.ImportRow(dr);}
//pass from holder to datatable
datatable = holder.Copy();
holder.Clear();
}
public void showDT()
{
dataGridView1.DataSource = datatable;
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// choose your column here
}
private void btn_Clicked(object sender, EventArgs e)
{
Find('YourColumn, 'your search string);
showDT();
}

Add a new row to datagrid view and reflect the changes in database

I have a dgv which I bind to a datatable.
public Form1()
{
da = new SqlDataAdapter("Select * from workers order by id", connStr);
cb = new SqlCommandBuilder(da);
dt = new DataTable();
}
Here I fill the datatable and link the gridview to the datasource
private void button1_Click(object sender, EventArgs e)
{
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
When I want to add a new row to the datagridview I do the following:
private void button2_Click(object sender, EventArgs e)
{
dt.Rows.Add(dt.NewRow());
da.Update(dt); //reflects changes to database
dt.Clear();
da.Fill(dt);
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;
}
Is there a way to add the row without having to reload the whole table?

error in c# when use the loop

I have this error c#
foreach statement cannot operate on variables of type int because int does not contain a public definition for GetEnumerator.
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = ord.GET_ORDER_DETAILS(textBox1.Text);
DataTable dt = new DataTable();
foreach(var r in dataGridView1.Rows.Count)
{
dt.Rows.Add(r.Cells[0].Value, r.Cells[1]);
}
}
You want to iterate the rows, not the count of the number of rows.
foreach(var r in dataGridView1.Rows)
{
dt.Rows.Add(r.Cells[0].Value, r.Cells[1]);
}
Remove the Count and try
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = ord.GET_ORDER_DETAILS(textBox1.Text);
DataTable dt = new DataTable();
foreach(var r in dataGridView1.Rows)
{
dt.Rows.Add(r.Cells[0].Value, r.Cells[1]);
}
}
I think you don't need to add each of rows, just:
DataTable dt = (DataTable)dataGridView1.DataSource;
If you only want 2 columns then try
var dtResult = ord.GET_ORDER_DETAILS(textBox1.Text);
dataGridView1.DataSource = dtResult;
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
foreach (DataRow item in dtResult.Rows)
{
dt.Rows.Add(item["Column1"], item["Column2"]);
}

add selected row from one gridview to another

I have 2 GridView controls. I need to add the selected row from one GridView to a second GridView. (I select row from GridView by clicking direct to GridView.)
Here's my code, but it copies all data from gridview1 to gridview2. I need the selected row only.
private void button6_Click(object sender, EventArgs e)
{
DataGridViewColumn newCol = null;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
newCol = new DataGridViewColumn(col.CellTemplate);
newCol.HeaderText = col.HeaderText;
newCol.Name = col.Name;
dataGridView2.Columns.Add(newCol);
}
dataGridView2.RowCount = dataGridView1.RowCount;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dataGridView2.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
}
}
I populated dataGridView1 with a DataTable:
SqlConnection con = new SqlConnection(#"Data Source=AFZAL\SQLEXPRESS;Initial Catalog=GIMS_LabInfo;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("SELECT PROFCODE,PROFNAME FROM PROFNAMES$ WHERE (PROFNAME LIKE '" + textBox1.Text + "%') AND PROFCODE NOT IN (SELECT PROFCODE FROM MAP) ORDER BY Profname desc ", con);
sc.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(sc);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
Immediately after setting dataGridView1.DataSource:
dataGridView1.DataSource = yourDataTable;
Set dataGridView2.DataSource to clone the structure (such as Columns) of the first DataTable:
dataGridView2.DataSource = yourDataTable.Clone();
Or:
dataGridView2.DataSource = ((DataTable)dataGridView1.DataSource).Clone();
Then in your button click event, all you need is this:
private void button6_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow == null)
return;
var currentRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
((DataTable)dataGridView2.DataSource).ImportRow(currentRow);
}
How can I add multiple rows on each click? As right now when I select second row, it replaces the First one in dataGridView2.
If you have multiple selected rows, you'll have to iterate over the SelectedRows property. Try this:
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow == null)
return;
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
((DataTable)dataGridView2.DataSource).ImportRow(((DataRowView)row.DataBoundItem).Row);
}

How to Add table content to combobox?

This is what I have right now, this code is Just Adding the table name into the Combobox not the customerID. Let say CustomerID has 1,2,3,4,5 I want to be able to add each ID into the combobox
how would I do this?
What I have right now:
private void Form2_Load(object sender, EventArgs e)
{
ds = new DataSet();
dc = new DataService();
ds.Tables.Add(dc.GetData("Select * from Customers", "CustomerID"));
foreach (DataTable dt in ds.Tables)
{
this.comboBox1.Items.Add(dt.TableName);
}
}
You Can Use this
DataTable dt = new DataTable();
dt = ds.Tables[0];
foreach (DataRow item in dt.Rows)
{
// do what you want here
this.comboBox1.Items.Add(item["CustomerID"]);
}
Replace:
this.comboBox1.Items.Add(dt.TableName);
With:
foreach (DataRow row in dt.Rows)
{
this.comboBox1.Items.Add(row[0].ToString());
}
This should do the trick:
private void Form2_Load(object sender, EventArgs e)
{
ds = new DataSet();
dc = new DataService();
DataTable td = dc.GetData("Select * from Customers", "CustomerID");
foreach (DataRow dr in td.Rows)
{
this.comboBox1.Items.Add(dr["CustomerID"]);
}
}

Categories

Resources