I have the data below, and I used RowFilter to filter the ID, and only show rows that have ID = 111. However, I need to create a "Next" button to be able to go to the next unique ID which is 222. The IDs are not incremental.
Original Table:
Any tips on how to approach this? I am running out of options
string[] columnnames = file.ReadLine().Split('|');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dt.NewRow();
string[] values = newline.Split('|');
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
dv.RowFilter = "ID = '111'";
dataGridView1.DataSource = dv;
I used dv.RowFilter = "ID = '111'"; however how can I make it dynamic so it can go to the next ID= 222?
Thanks
Related
I am writing a program that involves an PrintListView, I have gotten the Data to show, however each part of the Data is on a different Row. The Image marked as [1] below contains my PrintListView, which shows the Data on each different row, I want this Data to be on 1 row only. I am taking a DataSet, which I then turn into a DataTable. I then add the rows of each DataTable to a List, I have checked this in debug, and it only shows up with 3 values, which is great; but they are not on the same row. The Programming Language is C#.
https://i.stack.imgur.com/XAvEq.png [1]
If you could help, that would be handy.
Many Thanks.
public ObjectListView CreateOLVForSingleColumns(DataSet ds)
{
var olv = new ObjectListView();
olv.ShowGroups = false;
var colHeaders = new List<ColumnHeader>();
var objectList = new List<DataRow>();
//Setting the Column Names of the OLV
var totalOrders = new OLVColumn {AspectName = "Total_Orders", Text = "Total Orders", Width = 250};
var totalOrderValue = new OLVColumn {AspectName = "Total_Order_Value", Text = "Total Order Value", Width = 250};
var totalOrdersNotInvoiced = new OLVColumn {AspectName = "Total_Value_Of_Orders_Not_Invoiced", Text = "Total Order Value Not Invoiced", Width = 250};
colHeaders.AddRange(new ColumnHeader[] {totalOrders, totalOrderValue, totalOrdersNotInvoiced});
olv.Columns.AddRange(colHeaders.ToArray());
var dt = ds.Tables["Total Amount of Orders"];
var dt2 = ds.Tables["Total Value of Orders"];
var dt3 = ds.Tables["Total Value of Orders Not Invoiced"];
var dt4 = new DataTable();
dt4.Merge(dt);
dt4.Merge(dt2);
dt4.Merge(dt3);
for (int i = 1; i <= dt4.Rows.Count; i++)
{
var value = dt4.Rows[0];
var value2 = dt4.Rows[1];
var value3 = dt4.Rows[2];
olv.AddObjects(new DataRow[] { value, value2, value3 });
}
I think your problem is with DataTable.Merge not with ObjectListView.
I would suggest changing your data source so it all comes back in one table (e.g. by using a JOIN if your reading SQL). That said you can accomplish what you want with something like this:
var dt4 = new DataTable();
//add all the columns
foreach (DataColumn c in
dt.Columns.Cast<DataColumn>()
.Union(dt2.Columns.Cast<DataColumn>())
.Union(dt3.Columns.Cast<DataColumn>()))
{
dt4.Columns.Add(c.ColumnName, c.DataType);
}
if(dt.Rows.Count != dt2.Rows.Count|| dt.Rows.Count != dt3.Rows.Count)
throw new Exception("DataTables had different row counts");
for (int i = 0; i < dt.Rows.Count; i++)
{
var row = dt4.Rows.Add();
foreach (DataColumn c in dt.Columns)
if (dt.Rows[i][c] != DBNull.Value)
row[c.ColumnName] = dt.Rows[i][c];
foreach (DataColumn c in dt2.Columns)
if (dt2.Rows[i][c] != DBNull.Value)
row[c.ColumnName] = dt2.Rows[i][c];
foreach (DataColumn c in dt3.Columns)
if (dt3.Rows[i][c] != DBNull.Value)
row[c.ColumnName] = dt3.Rows[i][c];
}
If you run that code with the setup code:
var ds = new DataSet();
var dt = ds.Tables["Total Amount of Orders"] ?? new DataTable();
dt.Columns.Add("A");
dt.Rows.Add("A1");
dt.Rows.Add("A2");
var dt2 = ds.Tables["Total Value of Orders"]?? new DataTable();
dt2.Columns.Add("B");
dt2.Rows.Add("B1");
dt2.Rows.Add("B2");
var dt3 = ds.Tables["Total Value of Orders Not Invoiced"]?? new DataTable();
dt3.Columns.Add("C");
dt3.Rows.Add("C1");
dt3.Rows.Add("C2");
You get
If you use Merge then you get
I want to populate some fields on a PDF with data from a SQL database, I have a data access class that works but I can't get the values from the rows:
DataAccess DA = new DataAccess();
DataSet S = DA.CashData();
DataTable Cash = S.Tables[0];
DataTable Other = S.Tables[1];
DataTable Check = S.Tables[2];
DataTable Else = S.Tables[3];
string s = "";
foreach (DataRow row in Cash.Rows)
{
foreach (DataColumn col in Cash.Columns)
{
for (int i = 0; i < Cash.Columns.Count; i++)
{
DataRow cashRow = Cash.Rows[i];
s = s + cashRow[0].ToString() + ";";
}
}
Console.ReadLine();
}
string[] data = s.Split(';');
cell32.AddElement(new Paragraph(""));
cell32.AddElement(new Paragraph(data[0]));
cell32.AddElement(new Paragraph(data[1]));
The columns show data but the rows do not
If I understand your source correctly, you have a wrong execution in your loop for the rows, please consider this edited source:
DataAccess DA = new DataAccess();
DataSet S = DA.CashData();
DataTable Cash = S.Tables[0];
DataTable Other = S.Tables[1];
DataTable Check = S.Tables[2];
DataTable Else = S.Tables[3];
string s = "";
foreach (DataRow row in Cash.Rows)
{
for (int i = 0; i < row.Columns.Count; i++)
{
s += row[0].ToString() + ";";
}
Console.ReadLine();
}
string[] data = s.Split(';');
cell32.AddElement(new Paragraph(""));
cell32.AddElement(new Paragraph(data[0]));
cell32.AddElement(new Paragraph(data[1]));
I want to add textBox to my column cells by for loop...
My code:
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("No");
dt.Columns.Add(dc);
dc = new DataColumn("Item");
dt.Columns.Add(dc);
dc = new DataColumn("Available Stock");
dt.Columns.Add(dc);
dc = new DataColumn("Quantity");
dt.Columns.Add(dc);
dc = new DataColumn("Price");
dt.Columns.Add(dc);
// Define rows
for (int i = 0; i < count; i++)
{
string no = dtr.Rows[i][0].ToString();
string item = dtr.Rows[i][2].ToString() + " " + dtr.Rows[i][1].ToString();
string A_qty = dtr.Rows[i][3].ToString();
string price = dtr.Rows[i][4].ToString();
dt.Rows.Add(no, item, A_qty,"[i want to add text box here] " ,price);
}
dataGridView1.DataSource = dt;
I want to add textBoxes to 4th column and I want access that one by one.
You can add textBox column to DataGridView instead and loop through DataGridView like
DataTable dt=new DataTable();
dt.Columns.Add("No",typeof(int));
dt.Columns.Add("Item",typeof(string));
dt.Columns.Add("quantity",typeof(int));
dt.Columns.Add("Price",typeof(decimal));
//Add row to the datatable
for (int i = 0; i < count; i++)
{
//Not Sure what dtr is you looping through
string no = dtr.Rows[i][0].ToString();
string item = dtr.Rows[i][1].ToString() + " " + dtr.Rows[i][1].ToString();
string A_qty = dtr.Rows[i][2].ToString();
string price = dtr.Rows[i][3].ToString();
dt.Rows.Add(no, item, A_qty, price);
}
//Create New DataGridViewTextBoxColumn
DataGridViewTextBoxColumn textboxColumn=new DataGridViewTextBoxColumn();
//Bind DataGridView to Datasource
dataGridView1.datasource=dt;
//Add TextBoxColumn dynamically to DataGridView
dataGridView1.Columns.Add(textboxColumn);
//Loop through DataGridView
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//Do your task here
string fourthColumn = row.Cells[4].Value.toString();
}
Not sure. But hope this code will help you. Just add column of type textbox to your datatable.
DataTable table = new DataTable();
DataColumn col = new DataColumn("Name", typeof(TextBoxBase));
table.Columns.Add(col);
I have the following code, its a custom people picker for sharepoint 2010.
It searches by username, but also by the person name.
Because its a contains search, if I try with part of my username: cia
It shows my duplicated rows because that matches the username but also the person name.
this is my code (I cant use LINQ:
protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize)
{
try
{
// Find any user that has a matching name
var table = ADHelper.ExecuteNameQuery(RootPath, search);
// 20249: Search by username, method was already done, but it was not being called.
var table2 = ADHelper.ExecutesAMAccountNameQuery(search);
table2.Merge(table,);
PickerDialog.Results = table2;
Normally the DataTable.Merge method removes duplicates implicitely. But only when all columns' values are the same.
I'm not sure if there is something simplier(you've mentioned that you cannot use LINQ), but you could merge both and remove the duplicates afterwards:
List<string> dupColumns = new List<string>();
dupColumns.Add("ColumnA");
dupColumns.Add("ColumnB");
table2.Merge(table,);
RemoveDuplicates(table2, dupColumns);
And here the remove-duplicates function:
private void RemoveDuplicates(DataTable table, List<string> keyColumns)
{
Dictionary<string, string> uniquenessDict = new Dictionary<string, string>(table.Rows.Count);
System.Text.StringBuilder sb = null;
int rowIndex = 0;
DataRow row;
DataRowCollection rows = table.Rows;
while (rowIndex < rows.Count)
{
row = rows[rowIndex];
sb = new System.Text.StringBuilder();
foreach (string colname in keyColumns)
{
sb.Append(((string)row[colname]));
}
if (uniquenessDict.ContainsKey(sb.ToString()))
{
rows.Remove(row);
}
else
{
uniquenessDict.Add(sb.ToString(), string.Empty);
rowIndex++;
}
}
}
you should the .ToTable function
here is a sample code
DataTable DT1 = new DataTable();
DT1.Columns.Add("c_" + DT1.Columns.Count);
DT1.Columns.Add("c_" + DT1.Columns.Count);
DT1.Columns.Add("c_" + DT1.Columns.Count);
DataRow DR = DT1.NewRow();
DR[0] = 0;
DR[1] = 1;
DR[2] = 2;
DT1.Rows.Add(DR);
DataTable DT2 = new DataTable();
DT2.Columns.Add("c_" + DT2.Columns.Count);
DT2.Columns.Add("c_" + DT2.Columns.Count);
DT2.Columns.Add("c_" + DT2.Columns.Count);
DT2.Columns.Add("c_" + DT2.Columns.Count);
DR = DT2.NewRow();
DR[0] = 0;
DR[1] = 1;
DR[2] = 2;
DR[3] = 3;
DT2.Rows.Add(DR);
DT1.Merge(DT2);
Trace.IsEnabled = true;
DataTable DT_3=DT1.DefaultView.ToTable(true,new string[]{"c_1","c_2","c_0"});
foreach (DataRow CDR in DT_3.Rows)
{
Trace.Warn("val",CDR[1]+"");//you will find only one data row
}
I have a DataView which has two columns: ContactID, and Name
How can I check if a particular ContactID is already existing in the DataView?
Have you had a look at DataView.FindRows Method or maybe DataView.Find Method
The DataView has a method called FindRows, this can be used to search for a specific contactID, for example...
var table = new DataTable();
var column = new DataColumn("Id", typeof (int));
table.Columns.Add(column);
table.PrimaryKey = new[] {column}; // Unique Constraint
var row = table.NewRow();
row["Id"] = 100;
table.Rows.Add(row);
row = table.NewRow();
row["Id"] = 200;
table.Rows.Add(row);
var view = new DataView(table) { ApplyDefaultSort = true };
var rows = view.FindRows(200);
foreach(var r in rows)
{
Console.WriteLine(r["Id"]);
}
Use the Following code to Find the row in the Dataview
//Your original Table Which consist of Data
DataTable dtProducts = new DataTable();
//Add the DataTable to DataView
DataView ProductDataView = new DataView(dtProducts);
ProductDataView.RowFilter = "";
ProductDataView.Sort = "ProdId";
int recordIndex = -1;
//In the Find Row Method pass the Column
//value which you want to find
recordIndex = ProductDataView.Find(1);
if (recordIndex > -1)
{
Console.WriteLine("Row Found");
}