Reputation: 43
I search all over the stackoverflow and unable to find a suitable answer for my problem. I wanted to bind datatable values to datagridview in windows form.Specially data table in one class and Gridview in Seperate file.
Here is my Code.
namespace MyProj
{
public partial class ThisAddIn
{
public string GetDetails()
{
// Some Codes here
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("uid");
dt.Columns.Add("email");
//Some codes here.I just only give a data table part only.
DataRow row = dt.NewRow();
row["id"] = sid;
sid++;
row["uid"] = uid;
row["email"] = e;
dt.Rows.Add(row);
}
}
}
I Just tried to add Gridview,here is that code. First i add Add -> NewItem -> WindowsForm & add as form1.cs
Then i add Gridview to this form1.cs class from toolbox.Then double click gridview.
Here is my form1.cs coding
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//ThisAddIn th = new ThisAddIn();
this.dataGridView1.Visible = true;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource =dt; // here show dt does not contain the current context.
}
Both files are under same namespace.When i tries to create a object from a class (ThisAddIn th = new ThisAddIn();)then it shows,
ThisAddIn.ThisAddIn(Microsoft.Office.tools.Outlook Factory factory,IsServiceProvider serviceProvider)
This AddIn does not contain a constructor that takes 0 arguments
I'm a fresher to c# and please help me to solve this problem,If you can give me a solution with explanation is great..
Upvotes: 3
Views: 59548
Reputation: 749
private void BindProductsGrid()
{
dataGridView1.Rows.Clear();
DataTable dt = new DataTable();
dt = bl.BindProducts();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i]["Product_id"].ToString();
dataGridView1.Rows[i].Cells[2].Value = dt.Rows[i]["Product_name"].ToString();
dataGridView1.Rows[i].Cells[3].Value = dt.Rows[i]["Quantity"].ToString();
}
}
}
Upvotes: 0
Reputation: 65672
1) The GetDetails method must return a DataTable, so I changed string
to DataTable
and return dt;
public partial class ThisAddIn
{
public DataTable GetDetails()
{
// Some Codes here
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("uid");
dt.Columns.Add("email");
DataRow row = dt.NewRow();
row["id"] = sid;
sid++;
row["uid"] = uid;
row["email"] = e;
dt.Rows.Add(row);
return dt;
}
}
2) Notice how I instantiate the ThisAddIn class, then I call the GetDetails method - returning the results into a DataTable who's scope is in context.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ThisAddIn th = new ThisAddIn();
//Declare a DataTable and call to GetDetails
DataTable dt = th.GetDetails();
this.dataGridView1.Visible = true;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dt;
}
3) When you instantiate ThisAddIn th = new ThisAddIn();
you get the error:
This AddIn does not contain a constructor that takes 0 arguments
To resolve this you need to supply some values (arguments in the parameter) when instantiating the class:
ThisAddIn th = new ThisAddIn(value1, value2, etc)
Upvotes: 4