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"]);
}
Related
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();
}
I have a Gridview and want to make header dynamically based on user selection.
How to create headers that's been selected from users.
DataTable will help you to achieve. Please follow the code below
DataClassesDataContext db = new DataClassesDataContext();
protected DataTable GetDataSource()
{
DataTable dt = new DataTable();
var questions = db.ExecuteQuery<string>("select question from quiz where quizid is 123").ToList();
// Header implementation
int count = 0;
foreach (var question in questions)
{
DataColumn dc = new DataColumn(question);
dt.Columns.Add(dc);
count++;
}
// Rows implementation here
DataRow row = dt.NewRow();
...
dt.Rows.Add(row);
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
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"]);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Name");
dt.Rows.Add("1","John");
dataGrid1.ItemsSource = dt.DefaultView;
}
how i can add new row, on click my button? thanks :)
Sample Code :
Dynamically create table, add cloumn, add rows
1- Create a new DataTable
DataTable dt = new DataTable ("Table_AX");
2- Add columns to the DataTable
// Method 1
dt.Columns.Add ("column0", System.Type.GetType ("System.String"));
// Method 2
DataColumn dc = new DataColumn("column1",System.Type.GetType("System.Boolean"));
dt.Columns.Add (dc);
3- To add rows to the DataTable
// Initialize the row
DataRow dr = dt.NewRow ();
dr ["column0"] = "AX";
dr ["column1"] = true;
dt.Rows.Add (dr);
// Doesn't initialize the row
DataRow dr1 = dt.NewRow ();
dt.Rows.Add (dr1);
private void button1_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id",System.Type.GetType ("System.String"));
dt.Columns.Add("Name",System.Type.GetType ("System.String"));
DataRow dr=dt.NewROw();
dr[0]="a";
dr[1]="abc";
dt.Rows.Add(dr);
dataGrid1.ItemsSource = dt.DefaultView;
}
Try this code
private void button1_Click(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr["id"]="testid";
dr["Name"] = "testname";
dt.Rows.Add(dr);
dataGrid1.ItemsSource = dt.DefaultView;
}
How can i insert each time a diffrent row in datatable?
The data table is used in a dataGridView to write a log. I need each time to write in a diffrent line (row) in the datatable, when I can't know how many rows I need in runtime.
Another thing; the datatable is loaded from a XML file, so there might be already rows in the data table.
What can i do? (in short, I always write in the same row) in C#
EDIT:
here is the code:
From some reason, the DateGridView isonly having one row(This is a method that activated in a click of a button)
public void gridStart(string i, string b, string c)
{
DataTable dt = new DataTable();//empty table with no schema
DataColumn colContactID = new DataColumn("Date", typeof(string));
DataColumn colContactName = new DataColumn("Caller", typeof(string));
DataColumn colResult = new DataColumn("Result", typeof(string));
dt.Columns.Add(colContactID);
dt.Columns.Add(colContactName);
dt.Columns.Add(colResult);
DataRow row = dt.NewRow();
row["Date"] = i;
row["Caller"] = b;
row["Result"] = c;
dt.Rows.Add(row);
}
You should be able to Use DataTable.NewRow(). There are several samples on that MSDN page. If you have more questions please provide some sample code to your answer.
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = MyDataTable;
}
private string _fileName = "MyCache.xml";
private DataTable _myDataTable;
public DataTable MyDataTable
{
get
{
if (_myDataTable == null)
{
_myDataTable = new DataTable();
if (File.Exists(_fileName))
_myDataTable.ReadXml(_fileName);
else
InitDataTable(_myDataTable);
}
return _myDataTable;
}
}
private void InitDataTable(DataTable table)
{
table.TableName = "MyTable";
table.Columns.Add("Date", typeof(DateTime));
table.Columns.Add("Caller", typeof(string));
table.Columns.Add("Result", typeof(string));
}
// Have your add code call this method!
private void AddValue(DateTime date, string caller, string result)
{
var row = MyDataTable.NewRow();
row["Date"] = date;
row["Caller"] = caller;
row["Result"] = result;
MyDataTable.Rows.Add(row);
}
protected override void OnClosed(EventArgs e)
{
if (_myDataTable != null)
_myDataTable.WriteXml(_fileName, XmlWriteMode.WriteSchema);
base.OnClosed(e);
}
If it is bound to a data source, you need to first get the data source,
// Assuming it's a DataTable
DataTable dt = ((DataTable)myDataGridView.DataSource);
insert rows to your data source (like what your method is doing in your post), then tell the view to refresh its contents.
So maybe something like this would work:
public void gridStart()
{
DataTable dt = new DataTable();
DataColumn colContactID = new DataColumn("Date", typeof(string));
DataColumn colContactName = new DataColumn("Caller", typeof(string));
DataColumn colResult = new DataColumn("Result", typeof(string));
dt.Columns.Add(colContactID);
dt.Columns.Add(colContactName);
dt.Columns.Add(colResult);
dataGridView1.DataSource = dt;
// Call method to insert values.
}
to start up the grid, and:
public void gridInsert(string i, string b, string c)
{
DataTable dt = (DataTable)myDataGridView.DataSource;
DataRow row = dt.NewRow();
row["Date"] = i;
row["Caller"] = b;
row["Result"] = c;
dt.Rows.Add(row);
// Call another method to refresh grid view.
}
to call when you want to insert data to your DataTable.
My solution:
public partial class _Default : System.Web.UI.Page
{
DataTable tb = new DataTable();
}
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
DataTable dt = new DataTable("table");
dt.Columns.Add(new DataColumn("NR", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Dihname", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("ing", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Cost", Type.GetType("System.String")));
Session["ss"] = dt;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
tb = (DataTable)Session["ss"];
DataRow r1 = tb.NewRow();
r1[0] = ViewState["dishid"].ToString();
r1[1] = ViewState["dishname"];
r1[3] = Label3.Text;
tb.Rows.Add(r1);
DataList5.DataSource = tb;
DataList5.DataBind();
}
}