I am new at this. I am trying to populate a datagrid from a table source. My code is attempting to do two things.
First it populates a dataGrid with columns from a table. Then it adds a column called "SELECT" at the end of the grid. This select is CheckBox. However, when I execute this code, it adds the "SELECT" column twice.
I want to see it once. What am I doing wrong?
private void BankFlow_Load(object sender, EventArgs e)
{
initMethod();
dataTable = getBankFlowData(globalConnection);
dataGridView1.DataSource = dataTable;
}
private static DataTable getBankFlowData(OracleConnection oc)
{
DataTable dt = new System.Data.DataTable();
try
{
OracleCommand od = oc.CreateCommand();
od.CommandText = "SELECT * FROM BANK_FLOW_SOURCE";
od.CommandType = System.Data.CommandType.Text;
OracleDataAdapter adapter = new OracleDataAdapter(od);
adapter.Fill(dt);
}
catch (Exception)
{
}
return dt;
}
private static void initMethod()
{
targetSystem = ConfigurationManager.ConnectionStrings["prototype"].ConnectionString.ToString();
Console.WriteLine("Target : {0}", targetSystem);
sourceSystem = ConfigurationManager.ConnectionStrings["qlprod8"].ConnectionString.ToString();
Console.WriteLine("Source : {0}", sourceSystem);
globalConnection.ConnectionString = sourceSystem;
globalConnection.Open();
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.HeaderText = "SELECT";
col.ReadOnly = false;
col.DefaultCellStyle.BackColor = Color.Beige;
dataGridView1.Columns.Add(col);
}
Your problem is that the databindingcomplete event happens more then you think. In fact anytime some data changes it will fire.
You need to add the column outside of the databindingcomplete event.
EDIT
Actually since you are databinding, you may want to consider adding the column to your datatable. You would do this before the binding the datatable to the grid. Essentially, just create a datacolumn named select, that has the type of boolean and then add the datacolumn to the datatable.
Related
I have asked a similar question here but in my case I need to store multiple sets of datasets for an unknown number of times, not just once. So that is why I am asking here again.
I have multiple datatables with 2 columns in each datatable. One of them is a bool column (checkbox values). The checkbox values are empty when the form loads so up to user to check or uncheck them. Upon updating the checkbox, user press button1 to save only checkbox values in a dataset and this dataset will be saved in a List.
These datatables will then empty out and the same steps repeat for an unknown number of times (form loads empty datatables, user update checkboxes, user press button1). I have used the method below. No errors but when I want to display the List value in datagridview1 in Form2, it was empty. Below is my code. Hope to get help, thanks!
Class 1.cs (where I initiated my List)
public static List<string> list = new List<string>();
Form1.cs
//Create dataset
private DataSet Getdataset()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Items", typeof(string));
dt1.Columns.Add("Status", typeof(bool));
dt1.Rows.Add("hello");
dt1.Rows.Add("hello");
ds.Tables.Add(dt1);
dgv1.DataSource = dt1;
dgv1.AllowUserToAddRows = false;
DataTable dt2 = new DataTable();
dt2.Columns.Add("Items", typeof(string));
dt2.Columns.Add("Status", typeof(bool));
dt2.Rows.Add("bye");
dt2.Rows.Add("bye");
ds.Tables.Add(dt2);
dgv2.DataSource = dt2;
dgv2.AllowUserToAddRows = false;
return ds;
}
//Save dataset in a List
private void button1_Click(object sender, EventArgs e)
{
DataSet dd = Getdataset();
foreach (DataTable table in dd.Tables)
{
foreach (DataRow row in table.Rows)
{
Class1.list.Add(Convert.ToString(row["Status"]));
}
}
Form2 f = new Form2();
f.ShowDialog();
}
Form2.cs
//Display dataset in datagridview
private void compile_VisibleChanged(object sender, EventArgs e)
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
foreach (string item in Class1.list)
{
row.Cells[0].Value = item;
}
dataGridView1.Rows.Add(row);
}
EDIT
As per er-sho comment, I added a messagebox in the code per below and it showed Systems.Collection.Generic.List1[System.String] 7 times (I have 6 checkboxes). There was still no display in the datagridview1 and I initialized the no. of columns, headers for datagridview1 to avoid compilation error. However, see below comment under Gokham’s solution (seems like the data in List is not per expected)
foreach (string item in Class1.list)
{
MessageBox.Show(Class1.list.ToString());
row.Cells[0].Value = item;
}
Everytime, when you click button1, you rewrite your data in dgv1 with default values:
DataTable dt1 = new DataTable();
dt1.Rows.Add("hello");
dt1.Rows.Add("hello");
dgv1.DataSource = dt1;
dt1 will be contains rows with only Item values, but not with Status. You should add code with getting user inputs
UPDATE
To achieve the goal you should make follow steps:
Init dgv1 - add columns and fill rows with default values
private void Form1_Load(object sender, EventArgs e)
{
UpdateDgv1();
}
private void UpdateDgv1()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Items", typeof(string));
dt1.Columns.Add("Status", typeof(bool));
dt1.Rows.Add("hello");
dt1.Rows.Add("hello");
ds.Tables.Add(dt1);
dgv1.DataSource = dt1;
dgv1.AllowUserToAddRows = false;
}
Get values from dgv1 on button click
private void button1_Click(object sender, EventArgs e)
{
for (var i = 0; i < dgv1.Rows.Count; i++)
{
list.Add(Convert.ToString(dgv1.Rows[i].Cells[1].Value));
}
//restore dgv1
UpdateDgv1();
Form2 f = new Form2();
f.ShowDialog();
//clear list with old values or comment it
//if you want to save history of user inputs
list.Clear();
}
Show list content on datagridview1 of Form2
private void Form2_VisibleChanged(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 1;
foreach (string item in Form1.list)
{
var newRow = dataGridView1.Rows.Add();
//if checkbox from Form1 not checked
if (item == string.Empty)
dataGridView1.Rows[newRow].Cells[0].Value = false.ToString();
else
dataGridView1.Rows[newRow].Cells[0].Value = item;
}
}
Your Getdataset() method doesn't fill Status column of the table. Try
private DataSet Getdataset()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Items", typeof(string));
dt1.Columns.Add("Status", typeof(bool));
DataRow dr = dt1.NewRow();
dr["Items"] = "Hello";
dr["Status"] = checkBox1.Checked;
dt1.Rows.Add(dr);
ds.Tables.Add(dt1);
dgv1.DataSource = dt1;
dgv1.AllowUserToAddRows = false;
...
return ds;
}
I recommend you using BindingList instead of List and assigning the list as the DataSource for dataGridView1.
public static BindingList<string> list = new BindingList<string>();
Form 2
private void compile_VisibleChanged(object sender, EventArgs e)
{
dataGridView1.DataSource = Class1.list;
}
I think using FormLoad event handler instead of VisibleChanged should be better in your case.
Have a look : Binding List<T> to DataGridView in WinForm
I am facing this issue, I have datagridview and a datatable.
VPfn_CreateDataGrid();//This fuction creates gridview columns
DataTable invoice_table = (DataTable)invoice_data.DataSource;
now First thing, datagridview is empty when form loads. What I am trying to do is adding data to datagridview via multiple textboxes and combomoxes and for that I am using datatable.
private void btn_add_Click(object sender, EventArgs e)
{
DataRow x = invoice_table.NewRow();
x["serial_number"] = tsr.Text.ToString();
x["item"] = combo_items.SelectedItem.ToString();
x["item_rate"] = tr.Text;
x["item_qty"] = tq.Text;
x["item_unit"] = combo_unit.SelectedItem.ToString();
x["item_vat"] = combo_vat.SelectedItem.ToString();
x["amount"] = ta.Text;
invoice_table.Rows.Add(x);
invoice_data.Refresh();
}
And the error is "Column 'serial_number' does not belong to table"
first you have to create a the data table with the specific column. while your datagridview is empty the DataTable also will be empty.
DataTable invoice_table = (DataTable)invoice_data.DataSource;
infront of this..
while loading your form you can create datatable with the columns.
DataTable invoice_table; //Global
private void load()
{
invoice_table = new DataTable();
invoice_table.Columns.Add("serial_number", typeof(int));
invoice_table.Columns.Add("item");
.....
}
after that
private void btn_add_Click(object sender, EventArgs e)
{
DataRow x = invoice_table.NewRow();
x["serial_number"] = tsr.Text.ToString();
x["item"] = combo_items.SelectedItem.ToString();
x["item_rate"] = tr.Text;
x["item_qty"] = tq.Text;
x["item_unit"] = combo_unit.SelectedItem.ToString();
x["item_vat"] = combo_vat.SelectedItem.ToString();
x["amount"] = ta.Text;
invoice_table.Rows.Add(x);
invoice_data.Refresh();
}
Try this...
So i have a form that looks like this:
Image Link
and I generate the datatable for the dataviewgrid in the load function:
private void loadEmpresas(){
MySqlConnection myConn = new MySqlConnection(gVariables.myConnection);
MySqlCommand command = new MySqlCommand("Select codempresa as 'Codigo', nomempresa as 'Nombre empresa' from contabilidad.empresas", myConn);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = command;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
the table in mysql looks like this:
SQL Table Image Link
so when it is running it shows this:
Now the problems that i have are that i have no idea how to modify the width of the columns, id like the entire table to cover that gray space and i want that on click it selects the entire row not just a cell. When it clicks the row i want to pupulate the rest of the textboxes but i have a problem with the click event which for testing purposes is this:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("clicked");
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
MessageBox.Show(row.Cells[0].Value.ToString() + row.Cells[1].Value.ToString());
}
else
{
MessageBox.Show("wat");
}
}
when I click it doesn't even show a messagebox sometimes, I seriously have no idea how to handle the click row event properly :C help please T_T
For updating the column widths try using DataGridViewColumn.width:
DataGridViewColumn column = dataGridView.Columns[0];
column.Width = 60;
DataGridViewColumn.Width property information.
To select the entire row you need to change the SelectionMode of the DataGrid to FullRowSelect:
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;
SelectionMode information.
I have a Database with 2 Tables. Now i want to search for Values in my Databases.
I use this GUI so the "User" can give Information to the Textbox and the searched Values should shown in my Datagridview
I use this Code:
private void TB_MSGHeadline_TextChanged(object sender, EventArgs e)
{
try
{
if (TB_MSGHeadline.Text.Equals(""))
{
}
else
{
DataView dv = new DataView(table);
dv.RowFilter = string.Format("MessageHeadline LIKE '%{0}%'", TB_MSGHeadline.Text);
dataGridView1.DataSource = dv;
}
}
catch (Exception i)
{
MessageBox.Show("" + i);
}
}
Fill the DataGridview
private void GetData(string selectCommand)
{
//Creating a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, ConnectionString);
//Create a command builder to generate SQL update, insert, and
//delete commands based on selectCommand. These are used to
//update the Database.
SqlCommandBuilder CommandBuilder = new SqlCommandBuilder();
//Populate a new data table and bind it to the BindingSource
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
BindingSource1.DataSource = table;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = BindingSource1;
GetData("SELECT * FROM Tabelle1");
}
(Found it in a video but dont work for my problem)
But i get the Error, that the row "MessageHeadline" wasnt found
So i want to know, how can i make a LIKE search for my SQL Database?
I have a datatable which I bound programmatically to dataGridview1
public Form1()
{
InitializeComponent();
dataTable = new DataTable();
dataTable.Columns.Add("name");
this.dataGridView1.DataSource = dataTable;
Then in some event handler I add new row in dataTable and do a refresh which doesn't work : grid stays empty!
DataRow dataRow = dataTable.NewRow();
dataRow["name"] = "John";
dataGridView1.Refresh();
I read here http://objectmix.com/dotnet/92001-datagridview-doesnt-refresh.html to use RaiseListChangedEvents but I can't see how since I have no bindingsource as far as I can see.
Am I obliged to use another control BindingSource just to do that pretty basic functionality ? What's the use of Refresh method for the GridView then ?
Note: I can add to a simple list with no problem.
Update: for the context, I am capturing the text in clipboard (that part works so no problem) and just put it in the grid. I have tried the invoke method as suggested but it still doesn't work:
public partial class Form1 : Form
{
public delegate void DelRefresh();
private ClipboardSpy cps;
private DataTable dataTable;
public override void Refresh()
{
dataGridView1.DataSource = dataTable;
}
public Main()
{
InitializeComponent();
cps = new ClipboardSpy(this);
cps.ClipboardChanged += new EventHandler(cps_ClipboardChanged);
dataTable = new DataTable();
dataTable.Columns.Add("Url");
this.dataGridView1.DataSource = dataTable;
}
void cps_ClipboardChanged(object sender, EventArgs e)
{
String clipboardText = cps.GetClipboardData();
DataRow dataRow = dataTable.NewRow();
dataRow["Url"] = clipboardText;
dataGridView1.Invoke(new DelRefresh(Refresh));
//listBox1.Items.Add(string.Format("{0:HH:mm:ss} " + clipboardText, DateTime.Now));
//listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
}
UPDATE
You neet to add
datatTable.add(dataRow);
after setting the name property or the row is not added to the table.
NewRow only creates a row object matching the table, it does not add it to the table.
actually, because the datatable is bound to your datagridview, all changes to the datatable should appear immediately in the datagridview.
What could be the reason why your view is not updating, is that you are making the changes to the datatable in a different thread than the UI thread.
What you need to do is, to call something like
dataGridView1.Invoke(new DelRefresh(Refresh));
with
public delegate void DelRefresh();
public void Refresh()
{
dataGridView1.DataSource = dataTable
}
which should make it work.
try
Instead of
dataGridView1.Refresh();
try
dataGridView1.DataSource = dataTable