In Coded UI, I am facing a problem with Filling my Data Table on the top of HTmlTable on the UI.
Actually it is taking a lot of time to fill the datatable when there are 1000 of rows on the UI Table. I am working like this :
DataTable TestDataTable = new DataTable();
for (int i = 0; i < Table.RowCount; i++)
{
HtmlRow hr = (HtmlRow)Table.Rows[i];
for (int k = 0; k < hr.CellCount; k++)
{
TestDataTable.Rows[i][k] = hr.Cells[k].FriendlyName;
}
}
Its working Fine but as said it takes a lot of time. So is there any way i could fill the dataTable FASTER ?
Thanks,
Aashish GUpta
Possibly moving TestDataTable.Rows[i] out of the inner loop as it may be doing a full table (i,k) index evaluation every time.
HtmlRow hr = (HtmlRow)Table.Rows[i];
DataRow dest = (DataRow)TestDataTable.Rows[i];
for (int k = 0; k < hr.CellCount; k++)
{
dest[k] = hr.Cells[k].FriendlyName;
}
Have altered the data type in the code above based on the asker's comment. As the member types within DataTable are not specified I have assumed HtmlRow for dest.
Related
I have a data table that contains 100 rows, I want to copy a range of rows(31st row to 50th row) to another data table.
I am following below logic.
DataTable dtNew = table.Clone();
for(int k=30; k < 50 && k < table.Rows.Count; k++)
{
dtNew.ImportRow(table.Rows[k]);
}
Is there any better approach to do this?
Using LINQ you can do something like:
DataTable dtNew = table.Select().Skip(31).Take(20).CopyToDataTable();
Performance wise, using LINQ wont do any better, it however makes it more readable.
EDIT: Added handling check
int numOFEndrow = 20;
int numOfStartRow = 31;
if (table.Rows.Count > numOFEndrow +numOfStartRow)
{
DataTable dtNew = table.Select().Skip(numOfStartRow).Take(numOFEndrow).CopyToDataTable();
}
If it's about readability, then a good idea would be to throw this into an extension method.
Without changing your logic:
public static class Utils
{
public static void CopyRows(this DataTable from, DataTable to, int min, int max)
{
for (int i = min; i < max && i < from.Rows.Count; i++)
to.ImportRow(from.Rows[i]);
}
}
Then you can always reuse it without all the fancy syntax and know that it does exactly what you need if there is a concern of performance:
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.CopyRows(dt2, 30, 50);
I have a record in a dataTable as shown below.
1 Test 7Dec2014 15:40 one,two,three
Since the last column has 3 comma separated values, the resultant DataTable should like below with replicated records.
1 Test 7Dec2014 15:40 one
2 Test 7Dec2014 15:40 two
3 Test 7Dec2014 15:40 three
Please help me with an optimized way to achieve the above result.
The optimized way I found for the above problem is as below. If anybody has a better solution please let me know.
string[] strValues;
for (int i = 0; i < dtTable.Rows.Count; i++)
{
strValues= dtTable.Rows[i]["Column_Name"].ToString().Split(',');
if (strValues.Length > 1)
{
dtTable.Rows[i]["Column_Name"] = strValues[0];
for (int j = 1; j < strValues.Length; j++)
{
var TargetRow = dtTable.NewRow();
var OriginalRow = dtTable.Rows[i];
TargetRow.ItemArray = OriginalRow.ItemArray.Clone() as object[];
TargetRow["Column_Name"] = strValues[j];
dtTable.Rows.Add(TargetRow);
}
}
}
I have gone through this site but could not find the relative solution to my problem.
Here is my problem
I am creating a PDF file using iTextSharp and the file is being created very well. I am creating a table and assigning it a gridview data as follows.
//Add Actual columns from the datatable
for (int j = 0; j < dt.Columns.Count; j++) {
PdfPCell cellHeader = new PdfPCell(FormatHeaderPhrase(dt.Columns[j].ColumnName.ToString()));
cellHeader.HorizontalAlignment = 1;
table3.AddCell(cellHeader);// adds the header with proeprties
}
//Add the actual rows from the datatable
for (int i = 0; i < dt.Rows.Count; i++) {
for (int k = 0; k < dt.Columns.Count; k++) {
PdfPCell cellRows = new PdfPCell(FormatPhrase(dt.Rows[i][k].ToString().Replace("<br/>","\n").Replace("<sup>","")));
if (k != 2) {
cellRows.HorizontalAlignment = 1;
cellRows.VerticalAlignment = Element.ALIGN_MIDDLE;
}
else {
cellRows.HorizontalAlignment = 0;
}
table3.AddCell(cellRows);
table3.SplitRows = true;
table3.SplitLate = true;
}
}
I have a A3 page size. The concern here is, when my data in the table exceeds the page size, it trims the exceeded part and just shows the data which fits in one page. How to make it multipage(auto page break) based on the content of the table. Currently I am seeing the data which fits into one page of pdf. It should have generated more than one pages. How can I achieve this.
Please let me know your inputs. I would appreciate them. Thank you.
This may work, try creating new page after every n rows, suppose if current page fits only
10 rows, creating new page after every 10th record
Suppose you created a initial document instance
Document document =new Document(PageSize.A4);
After n rows, add new page by calling
document.NewPage();
What's the fastest way in term of speed of coding to add rows to a DataTable? I don't need to know neither the name of columns nor datatype. Is it possible to add rows without previously specify the number or name of dataTable columns?
DataTable t = new DataTable();
t.Rows.Add(value1,
value1,
value2,
value3,
...
valueN
);
DataSet ds = new DataSet();
ds.Tables.Add(t);
If the input comes out of a collection, you could loop it to create the DataColumns with the correct type:
var data = new Object[] { "A", 1, 'B', 2.3 };
DataTable t = new DataTable();
// create all DataColumns
for (int i = 0; i < data.Length; i++)
{
t.Columns.Add(new DataColumn("Column " + i, data[i].GetType()));
}
// add the row to the table
t.Rows.Add(data);
To answer your first question: no, you have to have columns defined on the table. You can't just say, "Hey, make a column for all these values." Nothing stopping you from creating columns on the fly, though, as Mr. Schmelter says.
Without knowing the rows or columns (you first need to add columns, without that not possible)
for(int intCount = 0;intCount < dt.Rows.Count; intCount++)
{
for(int intSubCount = 0; intSubCount < dt.Columns.Count; intSubCount++)
{
dt.Rows[intCount][intSubCount] = yourValue; // or assign to something
}
}
I have to write a function that takes a list of string(words that vary in length), and an int(size of data set eg. int value of 4 will be 4 columns and four rows in table), and with this I must produce a crossword like block(block being the dataset) that will hold as many of the words in the list as possible, like a crossword they can cross each other if the letters match at the right places, and the words must be all mixed up, read in every direction(like a crossword puzzle).
I can't seem to find code to help me with this, so far I have the basic structure of the dataset, here it is, any help will be appreciated, thanks.
public WordsDs WordMixer(List<string> wordList, int size)
{
if ((wordList == null) || (size < 2))
{
return null;
}
//shuffle the words in the list so that they are in a random order
Random random = new Random();
var sortedList = wordList.OrderBy(i => random.Next()).ToList();
//create a dataset for the words
DataSet ds = new DataSet();
DataTable dt = new DataTable();
//add columns and rows according to the size parameter
for (int i = 0; i < size; i++)
{
dt.Columns.Add(i.ToString(), typeof(string));
}
for (int i = 0; i < size; i++)
{
dt.Rows.Add(i);
}
for (int i = 0; i < wordList.Count; i++)
{
}//for (int i = 0; i < wordList.Count; i++)
}
You could just use a two-dimensional array to hold the characters. I guess the tricky part is from the word list work out where there a letter is shared between two words. I guess start with the least frequently used letter and work from there!
Interesting Article
http://blogs.teamb.com/craigstuntz/2010/01/11/38518/
Stack Overflow Question may help (although in c++ - might be of use)
Best data structure for crossword puzzle search
Other links to code generators.
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=6082&lngWId=10
http://dotnetslackers.com/articles/net/Creating-a-programming-crossword-puzzle.aspx
One in c
http://pdos.csail.mit.edu/cgi-bin/theme-cword