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"]);
}
}
Related
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"]);
}
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);
}
This question already has answers here:
How do you databind to a System.Windows.Forms.Treeview control?
(3 answers)
Closed 8 years ago.
I have created a DataTable with three columns and i am adding rows to that DataTable using Data Rows. Then,I am adding the DataTable to a DataSet. Now, I just want to bind the DataSet to a TreeView.
DataTable dt = new DataTable();
dt.TableName = "Tree";
dt.Columns.Add("Name");
dt.Columns.Add("Project");
dt.Columns.Add("Experience");
List<Profile> list = new List<Profile>
{
new Profile{ Name="Boopathi", Project="NPD", Experience=1},
new Profile{ Name="Stephan", Project="Luxstone", Experience=1},
new Profile{ Name="Sri", Project="DellAsap", Experience=1}
};
var query = from s in list.AsEnumerable()
where s.Experience == 1
select s;
DataSet ds=new DataSet();
DataRow dr = dt.NewRow();
foreach (var t in query)
{
dr["Name"] = t.Name;
dr["Project"] = t.Project;
dr["Experience"] = t.Experience;
}
ds.Tables.Add(dt);
return ds;
Try this
if (ds.Tables.Count > 0)
{
TreeNode root= new TreeNode("Root Node");
foreach (DataRow row in ds.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["Name"].ToString());
root.ChildNodes.Add(NewNode);
}
}
DataTable dt=new DataTable();
DataTable dt1 = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
ds.Relations.Add("children", dt.Columns["GSICCodeID"], dt1.Columns["GSICCodeID"]);
if (ds.Tables[0].Rows.Count > 0)
{
tvSicCode.Nodes.Clear();
Int32 i = 0;
foreach (DataRow masterRow in ds.Tables[0].Rows)
{
TreeNode masterNode = new TreeNode((string)masterRow["Description"], Convert.ToString(masterRow["GSicCodeID"]));
tvSicCode.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode = new TreeNode((string)childRow["SICCodeDesc"], Convert.ToString(childRow["SICCodeID"]));
if (Convert.ToString(ds.Tables[1].Rows[i]["CarrierSICCode"]) != "")
childNode.Checked = true;
masterNode.ChildNodes.Add(childNode);
i++;
}
}
tvSicCode.CollapseAll();
}
here is a simple article also
using telerik :
using System.Data.SqlClient;using Telerik.Web.UI;
namespace RadTreeView_DataBindDataTable
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindToDataTable(RadTreeView1);
}
}
private void BindToDataTable(RadTreeView treeView)
{
SqlConnection connection = new SqlConnection(Properties.Settings.Default.NwindConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter("select CategoryID, CategoryName, Description from Categories", connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
treeView.DataTextField = "CategoryName";
treeView.DataFieldID = "CategoryID";
treeView.DataValueField = "Description";
treeView.DataSource = dataTable;
treeView.DataBind();
}
}
}
I want to Specific data from one data table to another I Tried this Data is copies in
Table but Not Displayed in DataList
Can you Suggest me any solution Please?
My Code
DataTable dt1 = new DataTable();
DataTable dt = frmbal.GetAllForum();
for (int i = 0; i < dt.Columns.Count;i++ )
dt1.Columns.Add(dt.Columns[i].Caption );
// dt1.Rows.Clear();
DataRow []dr=dt.Select("intParentThreadID="+ Request.QueryString["id"]);
dt1.Rows.Add (dr);
dt1.AcceptChanges();
DataList1.DataSource =dt1;
DataList1.DataBind();
this code is in Page Load Event
Thanks in Advance
Try this:
foreach (DataRow dr in dt.Rows) {
dt1.Rows.Add(dr.ItemArray);
}
There is no DataRowCollection.Add method which takes a DataRow[] and inserts all rows. The overload that takes an object[] is to insert one record with these fields.
So this doesn't work:
DataRow []dr=dt.Select("intParentThreadID="+ Request.QueryString["id"]);
dt1.Rows.Add (dr);
You should use DataTable.Merge and DataTable.Clone(to clone the table including columns).
DataTable dt = frmbal.GetAllForum();
DataTable dt1 = dt.Clone();
dt1.Merge(dt);
Vishal Suthar is good
.But must this code are inside of !IsPostBack
for sample is
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt1 = new DataTable();
DataTable dt = frmbal.GetAllForum();
for (int i = 0; i < dt.Columns.Count;i++ )
dt1.Columns.Add(dt.Columns[i].Caption );
// dt1.Rows.Clear();
DataRow []dr=dt.Select("intParentThreadID="+ Request.QueryString["id"]);
foreach (DataRow dr in dt.Rows) {
dt1.Rows.Add(dr.ItemArray);
}
dt1.AcceptChanges();
DataList1.DataSource =dt1;
DataList1.DataBind();
}
}
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;
}