Reputation: 395
I'm working on this little C# project for work and i need to add data to a DataGridView
control. I had it working before but sadly since i made some changes on my code, i don't remember how i had it before. All i remember is that in VisualStudio i was going back into the Form.Designer.cs
and changing some controls to public static
before testing so i could add rows. I read something online regarding that and they suggested to not do that so that's why i changed my code.
Anyhow moving forward, i have the main Class AutoCheck.cs
which has the methods:
public void addNASDestination(string[] info){
/*string[0] = Name
* string[1] = Path
* string[2] = Username
* string[3] = Password - Needs to be passed to XML encrypted. Not displayed in the table at all
*/
destinationsTable.Rows.Add(info[0], "NAS", info[1], info[2], info[3]);
destinationsTable.Update();
destinationsTable.Refresh();
checkTableRowCount();
}
public void addBDRDestination(string[] info){
/*string[0] = Name
* string[1] = Path
*/
destinationsTable.Rows.Add(info[0], "BDR", info[1]);
//destinationsTable.Update();
//destinationsTable.Refresh();
checkTableRowCount();
}
These methods once worked to add rows to the DataGridView
. The info
array value is passed from another Class called AddDialog.cs
from the method:
private void destAddButton_Click(object sender, EventArgs e)
{
ac = new AutoCheck();
if(destNameTextbox.TextLength <= 0 || destNameTextbox.Text == null){
MessageBox.Show("Please enter a name","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}else if(destPathTextbox.TextLength <= 0 || destPathTextbox.Text == null){
MessageBox.Show("Please select a path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}else if (!Directory.Exists(destPathTextbox.Text)){
MessageBox.Show("Please select a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}else if (isNAS())
{
if((destUserTextbox.TextLength <= 0 || destUserTextbox.Text == null) || (destPassTextbox.TextLength <= 0 || destPassTextbox.Text == null)){
MessageBox.Show("Please enter a Username and Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}else{
//If Name and User/Pass are good, add info to temp array and pass by reference to addNASDestination
string[] temp = new string[] { destNameTextbox.Text, destPathTextbox.Text, destUserTextbox.Text, AutoCheck.Encrypt(destPassTextbox.Text) };
ac.addNASDestination(temp);
this.Dispose();
}
}else{
//Assume its a BDR and add info to temp array and pass by reference to addBDRDestination
string[] temp = new string[] {destNameTextbox.Text,destPathTextbox.Text};
ac.addBDRDestination(temp);
this.Dispose();
}
}
AddDialog
is as the name describes, a dialog that asks the user for input, it then grabs the input and puts it in an array, passes the array by reference to addBDRDestination
or addNASDestination
and they should add the new row to the DataGridView
.
This is not working for me and i have tried to see if the data is even being sent over to addBDRDestination
or addNASDestination
by the use of Console.WriteLine
to output the data being passed and it is reaching those methods but the new row is not being added.
I tried refreshing the DataGridView
by adding this (This is also still on my posted code):
destinationsTable.Update();
destinationsTable.Refresh();
I also tried this tutorial: http://csharp.net-informations.com/datagridview/csharp-datagridview-add-column.htm Its about the same as what I'm doing now but it adds the whole array instead of breaking it down the way i do.
I also tried creating a DataRow
as shown here: https://social.msdn.microsoft.com/Forums/windows/en-US/f12158b3-4510-47cb-b152-409489c3a51a/how-to-add-rows-in-datagridview-programmatically?forum=winformsdatacontrols
DataRow dr = this.dt.NewRow();
dr["a"] = "ax";
dr["b"] = "add item";
destinationsTable.Rows.Add(dr);
I tried enabling and disabling AllowUserToAddRows
but that had no effect.
I also tried this:
DataGridViewRow row = (DataGridViewRow)destinationsTable.Rows[0].Clone();
row.Cells[0].Value = info[0];
row.Cells[1].Value = "BDR";
row.Cells[2].Value = info[1];
destinationsTable.Rows.Add(row);
I'm not too sure what else i can try as this worked for me before and now its not.
I think its worth mentioning that AddDialog.cs
and AutoCheck.cs
are different class/source files but are in the same namespace AutoCheck
.
In AddDialog.cs
im accessing methods from AutoCheck.cs
by adding AutoCheck ac = new AutoCheck();
. And the same goes for AutoCheck
to AddDialog
.
Is there any other way i can add rows? or maybe I'm doing something wrong with my current code? Many thanks!
Upvotes: 0
Views: 2388
Reputation: 1491
I am not sure which problem you are facing:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dgv.Columns.Add("cell_one", "Cell 1");
dgv.Columns.Add("cell_two", "Cell 2");
dgv.Columns.Add("cell_three", "Cell 3");
dgv.Columns.Add("cell_four", "Cell 4");
dgv.Columns.Add("cell_five", "Cell 5");
addNASDestination(new string[] { "1", "3", "4", "5" });
}
public void addNASDestination(string[] info)
{
/*string[0] = Name
* string[1] = Path
* string[2] = Username
* string[3] = Password - Needs to be passed to XML encrypted. Not displayed in the table at all
*/
dgv.Rows.Add(info[0], "NAS", info[1], info[2], info[3]);
//checkTableRowCount();
}
}
It compiles and works as expected.
Even this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable("main");
dt.Columns.Add("column_one", typeof(string));
dt.Columns.Add("column_two", typeof(string));
dt.Columns.Add("column_three", typeof(string));
dt.Columns.Add("column_four", typeof(string));
dt.Columns.Add("column_five", typeof(string));
dgv.DataSource = dt;
addAnyRow();
}
public void addAnyRow() {
var dt = (DataTable)dgv.DataSource;
var row = dt.NewRow();
row["column_one"] = "1";
row["column_two"] = "2";
row["column_three"] = "3";
row["column_four"] = "4";
row["column_five"] = "5";
dt.Rows.Add(row);
}
}
Here I seperated them:
namespace StackOverflow___46658777
{
public static class Global
{
public static DataGridView DestinationTable;
}
public partial class Form1 : Form
{
public Form1(bool dataTableOrManual = false)
{
InitializeComponent();
Global.DestinationTable = dgv;
if (dataTableOrManual) {
var dt = new DataTable("main");
dt.Columns.Add("column_one", typeof(string));
dt.Columns.Add("column_two", typeof(string));
dt.Columns.Add("column_three", typeof(string));
dt.Columns.Add("column_four", typeof(string));
dt.Columns.Add("column_five", typeof(string));
dgv.DataSource = dt;
new TAutoCheck().AddAnyRow();
new TAutoCheck().AddAnyRow();
new TAutoCheck().AddAnyRow();
} else {
dgv.Columns.Add("cell_one", "Cell 1");
dgv.Columns.Add("cell_two", "Cell 2");
dgv.Columns.Add("cell_three", "Cell 3");
dgv.Columns.Add("cell_four", "Cell 4");
dgv.Columns.Add("cell_five", "Cell 5");
new TAutoCheck().AddNASDestination(new string[] { "1", "3", "4", "5" });
}
}
}
public class TDialog : Form
{
public TDialog()
{
//anyButton.Click += validateRequest;
}
void validateRequest(object sender, EventArgs args)
{
new TAutoCheck().AddNASDestination(new string[] { "your", "validated", "strings", "are", "here" });
}
}
public class TAutoCheck
{
public TAutoCheck() { }
public void AddAnyRow()
{
var dt = (DataTable)Global.DestinationTable.DataSource;
var row = dt.NewRow();
row["column_one"] = "1";
row["column_two"] = "2";
row["column_three"] = "3";
row["column_four"] = "4";
row["column_five"] = "5";
dt.Rows.Add(row);
}
public void AddNASDestination(string[] info)
{
/*string[0] = Name
* string[1] = Path
* string[2] = Username
* string[3] = Password - Needs to be passed to XML encrypted. Not displayed in the table at all
*/
Global.DestinationTable.Rows.Add(info[0], "NAS", info[1], info[2], info[3]);
//checkTableRowCount();
}
}
}
Works as expected. I hope it is as near as you seperated them, because you did not tell much about how your communication is between your classes and DataGridView. Seperate the classes in own sourcefiles does not change the behaviour of the classes and functions inside.
Upvotes: 1