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
Related
I have a datagridview and I filled with data.
DataTable table = new DataTable();
dataGridView1.DataSource = table;
con = new SqlDataAdapter("SELECT * FROM TABLE "'", con);
ds = new System.Data.DataSet();
con .Fill(ds, "TABLE");
My problem is I have to add rows manually like the code below but it is just add one row.But what I need foreach's count row.
foreach (var a in names.Split(new char[] { ';' }))
{
DataRow newRow = table.NewRow();
table.Rows.Add(newRow);
dataGridView2.Rows[i + 1].Cells[3].Value = a.ToString();
i = i +1;
}
Try to use
DataTable dataTable = (DataTable)dataGridView2.DataSource;
DataRow drToAdd = dataTable.NewRow();
drToAdd[3] = a.ToString();
dataTable.Rows.Add(drToAdd);
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);
}
}
}
I am developing an application in which I want to insert the data from the DataSet.
Following is my code to Insert the data:
WebForm1.aspx.cs:
public void Recieve_Data()
{
DataGridView.DataSource = obj.Get_Data();
DataGridView.DataBind();
}
public void Insert_Data()
{
TextBox StudentID = DataGridView.FooterRow.FindControl("TxtID") as TextBox;
TextBox StudentName = DataGridView.FooterRow.FindControl("TxtName") as TextBox;
TextBox StudentSex = DataGridView.FooterRow.FindControl("TxtSex") as TextBox;
TextBox StudentCity = DataGridView.FooterRow.FindControl("TxtCity") as TextBox;
int StuID = Convert.ToInt32(StudentID.Text);
string StuName = StudentName.Text;
string StuSex = StudentSex.Text;
string StuCity = StudentCity.Text;
obj.Insert_Data(StuID, StuName,StuSex, StuCity);
Recieve_Data();
}
My Insert_Data() is:
public void Insert_Data(int StuID, string StuName, string StuSex, string StuCity)
{
adap = new SqlDataAdapter("select * from Student", con);
DataSet ds = new DataSet();
adap.Fill(ds,"Student");
DataRow dr = ds.Tables[0].NewRow();
dr["ID"] = StuID;
dr["Name"] = StuName;
dr["Sex"] = StuSex;
dr["City"] = StuCity;
ds.Tables[0].Rows.Add(dr);
ds.Tables[0].AcceptChanges();
}
I am successfully receiving the new inserted record (seventh record) in the dataset at last:
But in the database, my table is not updated with the seventh row:
public void Insert_Data(int StuID, string StuName, string StuSex, string StuCity)
{
adap = new SqlDataAdapter("select * from Student", con);
SqlCommandBuilder cmdBuilder=new SqlCommandBuilder(adap);
DataSet ds = new DataSet();
adap.Fill(ds,"Student");
DataRow dr = ds.Tables[0].NewRow();
dr["ID"] = StuID;
dr["Name"] = StuName;
dr["Sex"] = StuSex;
dr["City"] = StuCity;
ds.Tables[0].Rows.Add(dr);
ds.Tables[0].AcceptChanges();
adap.Update(ds, "Student");
}
// Note: To reflect these change in DB you have to call "Update" method of SqlDataAdapter and the update method need Insert/update/Delete command to perform these operation on DB. I have used SqlcommandBuilder to generate the Insert/update/Delete, The Student table must have primary key to generate the insert /update /delete command using SqlcommandBuilder.
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 have been working on an issue for a while and I finally figured it out. I was trying to set a control when the Form is being initialize. I am thinking that the control's value isn't being set is because the control has been drawn yet (CORRECT ME IF I AM WRONG).
My partial form code
//in form 1
....
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Owner = this;
form.Show(this);
}
....}
public Form2()
{
InitializeComponent();
setData(); //Sets a datagridview's combobox column and databinds a datatable
setGrid(); //Sets each row in the datagridview combobox's value to a string
}
private void setData()
{
gvTest.AllowUserToAddRows = false;
string strConn = "server=10.253.3.185;database=petersun-test1;user id=ctore;password=cqi$$;connection timeout=30";
SqlConnection conn = new SqlConnection(strConn);
DataTable dit = new DataTable();
try
{
conn.Open();
string sql = "SELECT LTRIM(RTRIM(COLUMN_NAME)) as ColumnName from INFORMATION_SCHEMA.COLUMNS where Table_Name='coproc' order by ORDINAL_POSITION";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dit);
DataGridViewComboBoxColumn dcDatabasefields = new DataGridViewComboBoxColumn();
dcDatabasefields.DataPropertyName = "ColumnName";
dcDatabasefields.HeaderText = "Database Field Name";
dcDatabasefields.Name = "dbFields";
dcDatabasefields.DisplayMember = "ColumnName";
dcDatabasefields.ValueMember = "ColumnName";
dcDatabasefields.Width = 200;
BindingSource bsourceFields = new BindingSource();
bsourceFields.DataSource = dit;
dcDatabasefields.DataSource = bsourceFields;
dcDatabasefields.DataSource = dit; // bsourceFields;
gvTest.Columns.Add(dcDatabasefields);
}
finally
{
conn.Close();
}
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("options");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["options"] = "A";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["options"] = "C";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["options"] = "D";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["options"] = "E";
dt.Rows.Add(dr);
gvTest.DataSource = dt;
}
private void setGrid()
{
foreach(DataGridView Row in gvGrid.Rows)
{
Row.Cells[0].Value = "string";
}
}
So what I think is happening is:
1. The component is getting initialized
2. Creates the datagridview with columns
3. Sets the column values
4. Draws the form
Is my thinking correct? My question is what form event should I set the control's value? I did some research and I am thinking on the PAINT event, but I am not sure either. Could someone explain the Form's life cycle in some detail or point me to somewhere.
Thanks
I just added the coded into the Form_load and it added the values to the grid