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();
}
}
}
Related
I am trying to load back values from a CSV to my class. Then display the values to my datatable. However, I get the error even after my values have been loaded into the class and placed inside the intended columns (See Figure 1). The error occurred at dt.Rows.Add(dr);. Below is my code:
public Newdatagrid()
{
InitializeComponent();
//Do datatable
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Bus Model", typeof(string));//0
dt.Columns.Add("Bus Type", typeof(string));//1
dt.Columns.Add("Mileage", typeof(string));//2
if (Savestate.vehnochange_list.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
dr["Bus Model"] = Savestate.busmodel_list[Savestate.busmodel_list.Count];//0
dr["Bus Type"] = Savestate.bustype_list[Savestate.bustype_list.Count];//1
dr["Mileage"] = Savestate.busmileage_list[Savestate.busmileage_list.Count];//2
}
dt.Rows.Add(dr);
this.dataGridView2.DataSource = dt;
}
}
I think you want something like this:
public Newdatagrid()
{
InitializeComponent();
//Do datatable
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Bus Model", typeof(string));//0
dt.Columns.Add("Bus Type", typeof(string));//1
dt.Columns.Add("Mileage", typeof(string));//2
if (Savestate.vehnochange_list.Count > 0)
{
for (int i=0; i < Savestate.vehnochange_list.Count; ++i)
{
DataRow dr = dt.NewRow();
dr["Bus Model"] = Savestate.busmodel_list[i];//0
dr["Bus Type"] = Savestate.bustype_list[i];//1
dr["Mileage"] = Savestate.busmileage_list[i];//2
dt.Rows.Add(dr);
}
this.dataGridView2.DataSource = dt;
}
}
private void AutoComplete()
{
textBox4.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox4.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
OleDbDataAdapter dAdapter2 = new OleDbDataAdapter("select ID from Car ", connParam);
OleDbCommandBuilder cBuilder2 = new OleDbCommandBuilder(dAdapter2);
DataTable dataTable2 = new DataTable();
DataSet ds2 = new DataSet();
dAdapter2.Fill(dataTable2);
foreach (DataRow row in dataTable2.Rows)
{
//TextBox1.Text = row["ImagePath"].ToString();
col.Add(row["ID"].ToString());
}
textBox4.AutoCompleteCustomSource = col;
}
I was calling this function in form load but not working
I also call the method on text_change but i faced the same problem that no values appeared
I have 5 rows in gridview in which I am trying to update one row, While updating, I wrote below code:-
protected void GrdProspective1_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
if (Session["grdEdit"] != null)
{
DtCombo = (DataTable)Session["grdEdit"];
}
else
{
BindDatatable();
}
DataRow[] dataProsUpdate = DtCombo.Select("SR_NO = " + e.Record["SR_NO"]);
dataProsUpdate[0]["PARTY_NAME"] = e.Record["PARTY_NAME"];
dataProsUpdate[0]["FLAT_NO"] = e.Record["FLAT_NO"];
dataProsUpdate[0]["LEASE_NUM"] = e.Record["LEASE_NUM"];
dataProsUpdate[0]["ACTION"] = e.Record["ACTION"];
dataProsUpdate[0]["NO_OF_DAYS"] = e.Record["NO_OF_DAYS"];
dataProsUpdate[0]["REMARKS"] = e.Record["REMARKS"];
GrdProspective1.DataSource = DtCombo;
GrdProspective1.DataBind();
AddToViewState("GrdProspective1");
}
But in this, I am getting error as
Index was outside the bounds of the array.
I am confused why datatable is emppty when there are 5 rows in the gridview
My datatable code:-
public void BindDatatable()
{
DataSet ds = new DataSet();
DataRow dr;
DataColumn SR_NO;
DataColumn PARTY_NAME;
DataColumn FLAT_NO;
DataColumn LEASE_NUM;
DataColumn ACTION;
DataColumn NO_OF_DAYS;
DataColumn REMARKS;
DtCombo = new DataTable();
SR_NO = new DataColumn("SR_NO", typeof(Int32));
PARTY_NAME = new DataColumn("PARTY_NAME", typeof(String));
FLAT_NO = new DataColumn("FLAT_NO", typeof(Int32));
LEASE_NUM = new DataColumn("LEASE_NUM", typeof(Int32));
ACTION = new DataColumn("ACTION", typeof(String));
NO_OF_DAYS = new DataColumn("NO_OF_DAYS", typeof(Int32));
REMARKS = new DataColumn("REMARKS", typeof(String));
DtCombo.Columns.Add(SR_NO);
DtCombo.Columns.Add(PARTY_NAME);
DtCombo.Columns.Add(FLAT_NO);
DtCombo.Columns.Add(LEASE_NUM);
DtCombo.Columns.Add(ACTION);
DtCombo.Columns.Add(NO_OF_DAYS);
DtCombo.Columns.Add(REMARKS);
}
Complete solution:
protected void GrdProspective1_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
if (Session["grdEdit"] != null)
{
DtCombo = (DataTable)Session["grdEdit"];
}
else
{
return;
}
DataRow[] dataProsUpdate = DtCombo.Select("SR_NO = " + e.Record["SR_NO"]);
dataProsUpdate[0]["PARTY_NAME"] = e.Record["PARTY_NAME"];
dataProsUpdate[0]["FLAT_NO"] = e.Record["FLAT_NO"];
dataProsUpdate[0]["LEASE_NUM"] = e.Record["LEASE_NUM"];
dataProsUpdate[0]["ACTION"] = e.Record["ACTION"];
dataProsUpdate[0]["NO_OF_DAYS"] = e.Record["NO_OF_DAYS"];
dataProsUpdate[0]["REMARKS"] = e.Record["REMARKS"];
GrdProspective1.DataSource = DtCombo;
GrdProspective1.DataBind();
AddToViewState("GrdProspective1");
}
public void DisplayGridEdit()
{
OracleCommand cmd = new OracleCommand("SELECT...", ObjPriCon);
DtCombo = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(DtCombo);
GrdProspective1.DataSource = DtCombo;
GrdProspective1.DataBind();
AddToViewState("GrdProspective1");
}
How to Pass data table from DataGridView ro ReportViewer ?
in a win Form I have a DataGridView control that fills from a SqlDataAdapter , and I added a check box column to it. for "ready to print"(or smt like this)
now I want to create a report with ReportViewr to show only checked rows in my DataGridView.
here is codes of my form1 (Contains datagridview)
private void Fill_DGV_Sefaresh()
{
try
{
MyDB db = new MyDB();//class that contains my SQL Connection
string sql = "SELECT sid, CONVERT(VARCHAR(10), cast(sdate as date), 111)sdate, sdes, sfor, suname, isdone, sqty FROM sefaresh order by sid desc";
SqlDataAdapter sda = new SqlDataAdapter(sql,db.MyConn);
DataSet ds = new DataSet();
BindingSource bs = new BindingSource();
sda.Fill(ds);
bs.DataSource = ds.Tables[0].DefaultView;
bindingNavigator1.BindingSource = bs;
dataGridView1.DataSource = bs;
}
catch { }
}
codes of my form2 (contains ReportViewer Control)
private SqlDataAdapter idata()
{
MyDB db = new MyDB();
string sql = "SELECT * FROM sefaresh";
SqlDataAdapter sda = new SqlDataAdapter(sql, db.MyConn);
return sda;
}
private void SefareshView_Load(object sender, EventArgs e)
{
idata().Fill(this.DS_Sefaresh.sefaresh);//DS_Sefaresh is a dataset item , sefaresh is a Table adapter
this.reportViewer1.RefreshReport();
}
Try something like the following,
ReportDataSource Rds = new ReportDataSource(dataSetName, bs);
reportViewer1.LocalReport.DataSources.Add(Rds);
Where bs is your BindingSource, dataSetName is the name of the dataset in the report, Rds is the datasource for the ReportViewer, and reportViewer1 is the ReportViewerControl.
If you for some reason need to take the data directly from the DataGridView just replace bs with dataGridView1.DataSource.
Segue
private void tbcaixas_Leave(object sender, EventArgs e)
{
Carga();
int caixas = Convert.ToInt32(tbcaixas.Text);
List<int> lista = new List<int>();
for (int i = 1; i <= caixas; i++)
{
DataRow linha = dt.NewRow();
linha["ncaixa"] = "1/" + i;
linha["loja"] = tbLoja.Text.ToUpper();
dt.Rows.Add(linha);
}
}
private void Carga()
{
dt = new DataTable();
dt.Columns.Add("ncaixa", typeof(string));
dt.Columns.Add("artigo", typeof(string));
dt.Columns.Add("tam", typeof(string));
dt.Columns.Add("cor", typeof(string));
dt.Columns.Add("arte", typeof(string));
dt.Columns.Add("qtd", typeof(string));
dt.Columns.Add("und", typeof(string));
dt.Columns.Add("loja", typeof(string));
dgvDados.DataSource = dt;
}
private void btnImprimir_Click(object sender, EventArgs e)
{
DataTable dtImpressao = ((DataTable)dgvDados.DataSource);
var parametro1 = new ReportParameter("data", dtpData.Text);
var parametro2 = new ReportParameter("cliente", tbCliente.Text);
var parametro3 = new ReportParameter("notafiscal", tbNotaFiscal.Text);
var parametro4 = new ReportParameter("pesobruto", tbPesoBruto.Text);
var parametro5 = new ReportParameter("pesoliquidado", tbPesoLiquidado.Text);
var parametro6 = new ReportParameter("pesoliquidado", cbEmpresa.SelectedItem.ToString());
ReportParameter[] paremetros = new ReportParameter[] { parametro1, parametro2, parametro3, parametro4, parametro5 };
ImpressaoHelper.Imprimir(null, "DataSetRelatorios", "br.com.bonor.Core.relatorios.pcp.Romaneio.rdlc", dtImpressao);
}
}
}
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"]);
}
}