Fórum Pegar valores das colunas de um DataTable #420986
03/08/2012
0
A instrução para exportar, que eu estou usando é essa, onde caminho é o path e mais o nome do arquivo e sua extensão(csv,xls,xlsx...):
using (StreamWriter sw = File.CreateText(caminho))
{
try
{
...
{
sw.WriteLine(dr[idendereco].ToString() + \t + dr[logradouro].ToString() + \t + dr[numero].ToString() + \t + dr[complemento].ToString() + \t + dr[cep].ToString() + \t + dr[sigla_uf].ToString() + \t + dr[cidade].ToString() + \t + dr[bairro].ToString());
}
Acontece que os caras: dr[idendereco] == dever ser uma coluna de meu DataTable, para poder montar os campos na planilha, célula por célula. Como eu faço? Como percorro um DataTable, pegando coluna a coluna?

Pjava
Curtir tópico
+ 0Posts
03/08/2012
Pjava
foreach(DataRow dw in tbl.Rows)
{
sw.WriteLine(dw[idendereco].ToString() + \t + dw[logradouro].ToString() + \t +dw[numero].ToString() + \t +dw[complemento].ToString() + \t +dw[cep].ToString() + \t +dw[sigla_uf].ToString() + \t +dw[cidade].ToString() + \t +dw[bairro].ToString());
}
Agora como eu faço para colocar os nomes nas colunas do excel? E se eu quiser preencher as colunas que faltam, na planilha onde eu faço as pesquisas? Tem como? Se tem, como eu faço?
Gostei + 0
03/08/2012
Joel Rodrigues
1 | DataTable result = tab.AsEnumerable().Where(r = r[COLUNA].ToString().Contains(textBox1.Text)).CopyToDataTable(); |
Neste exemplo, eu tenho a tabela tab que é a original e filtrei somente as linhas que contenham o valor do textbox na coluna especificada.
Gostei + 0
26/04/2016
Osvaldo Arreche
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | public bool tablesAreTheSame(DataTable table1, DataTable table2) { DataTable dt; dt = getDifferentRecords(table1, table2); if (dt.Rows.Count == 0) return true; else return false; } private DataTable getDifferentRecords(DataTable FirstDataTable, DataTable SecondDataTable) { //Create Empty Table DataTable ResultDataTable = new DataTable("ResultDataTable"); //use a Dataset to make use of a DataRelation object using (DataSet ds = new DataSet()) { //Add tables ds.Tables.AddRange(new DataTable[] { FirstDataTable.Copy(), SecondDataTable.Copy() }); //Get Columns for DataRelation DataColumn[] firstColumns = new DataColumn[ds.Tables[0].Columns.Count]; for (int i = 0; i < firstColumns.Length; i++) { firstColumns[i] = ds.Tables[0].Columns[i]; } DataColumn[] secondColumns = new DataColumn[ds.Tables[1].Columns.Count]; for (int i = 0; i < secondColumns.Length; i++) { secondColumns[i] = ds.Tables[1].Columns[i]; } //Create DataRelation DataRelation r1 = new DataRelation(string.Empty, firstColumns, secondColumns, false); ds.Relations.Add(r1); DataRelation r2 = new DataRelation(string.Empty, secondColumns, firstColumns, false); ds.Relations.Add(r2); //Create columns for return table for (int i = 0; i < FirstDataTable.Columns.Count; i++) { ResultDataTable.Columns.Add(FirstDataTable.Columns[i].ColumnName, FirstDataTable.Columns[i].DataType); } //If FirstDataTable Row not in SecondDataTable, Add to ResultDataTable. ResultDataTable.BeginLoadData(); foreach (DataRow parentrow in ds.Tables[0].Rows) { DataRow[] childrows = parentrow.GetChildRows(r1); if (childrows == null || childrows.Length == 0) ResultDataTable.LoadDataRow(parentrow.ItemArray, true); } //If SecondDataTable Row not in FirstDataTable, Add to ResultDataTable. foreach (DataRow parentrow in ds.Tables[1].Rows) { DataRow[] childrows = parentrow.GetChildRows(r2); if (childrows == null || childrows.Length == 0) ResultDataTable.LoadDataRow(parentrow.ItemArray, true); } ResultDataTable.EndLoadData(); } return ResultDataTable; } |
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)