ASP.NET: Datagrid CurrentRow - asp.net

I have a DataGrid, with around 100 differnt entries, I am wanting to loop through each row, and get a field form the database(the field is URL), into a string.Basically I have the code to do a screen scrape using the HTMLAgilityPack, but I want to do this for each row in the database, and update the database based upon the returned screen scrape.
Loop through each row
Store Each URL into a String
Use HtmlAgilityPack to do a screen scrape done
Update Database field "Price" with a value returned from step 3

You can loop through all the rows in your datagrid and add the value of a specific column to a list of strings.
var hyperlinks = new List<string>();
var indexOfColumn = 2;
foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
hyperlinks.Add(((DataGridViewCheckBoxCell)row.Cells[indexOfColumn]).Value);
}
Then use the hyperlinks list and update the database as you require in step 4.
I would say you could update the database inside the foreach statement and not bother adding to the list of hyperlinks, but for 100 rows that could be quite costly and unless you've got batching or something similar so you actually only hit the database every 20 updates or so.

Related

How to Get Individual Row for Each Submitted URL?

I have a HTML input box that I want to be able to submit URLs into. Only problem is that when I go to submit multiple urls, they are inserted into 1 row in the SQLite database in one big blob.
"http://google.com/http://stack.com/http://aol.com/"
When I want them to be:
Row 1: http://google.com/
Row 2: http://stack.com/
Row 3: http://aol.com/
Essentially, for every URL in the input box to be put into it's own individual row.
How can I achieve this?
This can be done in two steps.
Split the URL(consider it as a string) on the basis of a token ('/').
Then loop over the split string (as per example 3) and save it in DB.
Here is the example code.
var str = 'http://google.com/http://stack.com/http://aol.com/';
var str_array = str.split('/');

Row elements validation in Coded UI

I am working on Coded UI for an asp.net web application.
In one of tests I need to validate and verify if the elements of the row are populated right or not.
For instance, suppose my search criteria is such that the Serial Number should start from 001. As soon as I click on Search button my results grid gets populated with all the elements that have 001 in there serial number.
How can I validate that all the elements of the results grid are correct i.e. starting from 001? I know I must use .contains validation criteria.
But what code should I use to run the loop for checking each and every row? in c#.
This answer is heavily dependent upon what you mean by populated right
Is your grid rendering/CodedUI-Defined as a HtmlTable?
If your grid has strict columns you could just ask for the cells matching your criteria.
var cellDef = new HtmlCell(yourHtmlTable);
cellDef.SearchProperties.Add(HtmlCell.PropertyNames.ColumnIndex, "2");
cellDef.SearchProperties.Add(HtmlControl.PropertyNames.InnerText, "YourDynamicValue", PropertyExpressionOperator.Contains);
var matchingCells = cellDef.FindMatchingControls().OfType<HtmlCell>().ToArray();
Assuming your list is somewhat dynamic then you could just validate each entry exists. If you are trying to validate EVERY row, its contents, and formatting. If so you'll just have to cycle every row and ask for each cell according to your criteria.
foreach(HtmlRow row in yourTable.Rows)
{
var idCell = row.GetCell(0);
var decriptionCell = row.GetCell(1);
// your code to match your entries
}

Maintaining Row Selection in ASP.NET GridView Control

The Setup:
I currently have a page with a GridView control on it inside of an update panel, using a SqlDataSource. I have a timer setup to update the GridView every X amount of seconds. Typically for what I am testing every time the GridView updates about 4-5 new rows of data are added to the gridview, while the last 4-5 get tossed out. I am only displaying 15 results at a time and will have new results coming in every update.
The Problem:
I allow the user to select the rows, while the GridView is being updated. I am handling this by setting the SelectedIndex property. However, when I select a row and then the grid is updated the row the user selected gets pushed down about 4-5 rows and the data in the previous selected index is selected instead. So where they clicked is selected at this point, not what they clicked.
I need a way to determine, if possible from the SqlDataSource/Gridview, how many new rows have been added to the gridview. OR a way to maintain the selected data by the data in the row and not just the SelectedIndex.
Any help is appreciated, thanks.
RESOLVED:
Ok I went ahead and added a new invisible column to my grid, and am now keep track of the unique ID's selected from the DB. By setting an array before databinding, and comparing that to the new array I get after databinding I was able to use a simple Intersect to determine the number of rows that are the same. Then I used that to determine from the total how many are new this postback.
Just an idea:
I think you can use an invisible column (more specifically an ID column) to store the selected rows' IDs value in the Session object and then after the grid updates, you can retrieve this value(s) and select the row(s) again if they are still present.
If you have custom GridView OnRowUpdating event.
public void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Session["CurrIndex"] = GridView.SelectedIndex;//index before insertion
Session["RowCount"] = GridView.Rows.Count;//row count before insertion
//Add new Rows
GridView.SelectedIndex = (Int32)(Session["CurrIndex"]) + ( GridView.Rows.Count - (Int32)(Session["RowCount"]);//update selected index
Session["CurrIndex"] = GridView.SelectedIndex;//restore the index into session
}

gridview asp.net

I have a gridview which was working fine with a small dataset in development. In production it has to bind to thousands of records, which is making it slower to load. Is there a way to improve performance, like retrieving the data during gridview pageindex changing?
Also chances are you only want to bind it once. So you should (if not already):
if(!IsPostback)
{
DatabindGridLogicHere();
}
This way your GridView will only have to hit the db the first time to get the data.
First and formost turn off ViewState.
You should tell your datasource to take less records and then enable paging in your grid and datasource.
You can enable "AllowPaging" property to true in your GridView and give a page size say 10. Then Write your data retrieval logic to return a batch of data instead of the whole set of data at once.
When you write the SQL query make sure to order it by an ID.
Thus if the page index is 1 you can take the first batch of data by passing page index of 1 and the page size of 10.
Logic will be;
SELECT [RequiredFields]
FROM [YourDataSource]
WHERE (Id>=((PageIndex-1)*pageSize) AND Id<(PageSize*pageIndex)) ORDER BY Id
In the first page it will return first set of entries of those Ids starting from 0 to 9. In the second page it returns entries of those Ids starting from 10 to 19 assuming the pageSize is 10. You can change the page size and the query logic as you wish.
But if sorting is enable then this will not produce accurate results.

Get selected value from rowlist and pass it to query string laong with page number in JqGrid

I have a row list as [10,20,30] i want to pass the selected row list ie if (20 is selected ill pass that 20 as query string and retrieve the value). and also i want to select the page number too. What are the possible ways to achieve the selected value from row list and current page number and then pass the value to query string.
I tried var Page=$(".ui-pg-input").val()
but it is not firing i think i am missing something out here.
I got it done. By using the predefined session values in it

Resources