Unable to use int array item in table cell - c#

I am trying to use int array element in tablecell item and it doesn't allow me to add.
Code:
I'm getting datatable column values into array
int[] price = dtPrice.Rows.OfType<DataRow>().Select(n => Convert.ToInt32(n[0])).ToArray();
Then using foreach I am adding the items to the cell text and its not working.
foreach(var item in price)
{
string strValue = e.Row.Cells[item].Text;
}
Below error is what I see. Please provide any suggestions on how this can be handled.

Related

Adding a column string to a list for each selected Row in a DataGridVIew

I want to add the 'OrderID' in the below DataGridView to a list for each row that I select.
I have managed to get it to cycle through a foreach loop but I am trying to figure out how to specify the selected row index, as it currently pulls the same indexed 'OrderID' and not the different ones selected.
See result I am currently getting:
See current code:
List<string> orders = new List<string>();
foreach (var row in grid.SelectedRows)
{
orders.Add(grid.Rows[grid.SelectedRows[0].Index].Cells[0].Value.ToString());
}
I know I shouldn't be using SelectedRows[0], but I cant think of how to index the specific 'row'
I used a for loop instead:
for (int i = 0; i < grid.SelectedRows.Count; i++)
{
orders.Add((grid.Rows[i].Cells["Order ID"].Value).ToString());
}

Display the content of a list on a dynamically changing gridview

I have (asp) GridView with an item template inside. There are ten item templates with one textbox each. The GridView is bound in the behind code. A data table is made and depending on certain criteria only certain columns will be visible. This is how the table changes dynamically based on user criteria.
This is the code I use to collect the values inside those textboxes.
List<string> myList = new List<string>();
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
if (GridView1.Columns[i].Visible == true)
{
TextBox tb = (TextBox)row.FindControl($"TB{i}");
if (tb.Text == string.Empty)
{
myList.Add("Null Value");
}
else
{
string someVariableName = tb.Text;
myList.Add(tb.Text);
}
}
}
}
The TB{i} references the text boxes in the item template. They are named TB0 - TB9. This code will take the values and combine them into the list. That list will be stored in the database.
The issue I'm facing is redistributing those values back to the text boxes after that string has been queried from the database. Essentially, the string is queried and stored in a variable. That variable is then split on a comma delimiter and stored inside a new list. Here is the code I have so far, this is where the problem is.
TextBox dbInput = new TextBox();
dbInput.Text = "box1,box2,box3,box4,box5,box6,box7,box8,null,null,null,null,null,null,null,null,box13,null,null,null,null,null,null,null,null,11,222,null,null,null,null,null,null,null,null,null,null,null,null,333,null,null,null,null,444,null,333,1343";
List<string> stringList = dbInput.Text.Split(',').ToList();
int counter = 1;
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
if (GridView1.Columns[i].Visible == true)
{
TextBox tb = (TextBox)row.FindControl($"TB{i}");
tb.Text = stringList[i];
}
counter = counter + 1;
}
}
The input is hard coded in this instance (dbInput). I'm fairly certain the issue is this line tb.Text = stringList[i];. The reason for that is because there should only be four columns displayed with this output. It only prints the first four entries of the list into the first four boxes on row one. Then it goes to row two and prints the same four entries, repeat.
I have tried changing tb.Text = stringList[i]; [i] to another variable and incrementing it. I placed the variable outside the foreach loop but incremented it right after the inner for loop. This resulted everytime in this error Index was outside the bounds of the array.
Line 854.
Line 852: {
Line 853: TextBox tb = (TextBox)row.FindControl($"TB{i}");
Line 854: tb.Text = stringArray[counter];
Line 855: }
Line 856: counter = counter + 1;
How can I alter the code to loop through the contents of the list showing (for example) the first four items on row one, the next four items on row two and repeat till the list is empty?

Merge to IList<IWebElement> element into a Dictionary

I'm trying to create a dictionary from a two IWebelement that I obtain from selenium, the first element is the header of a table and the second one is the value of each column. It's possible to do it?
This are my two Ilist:
IList<IWebElement> headersrows = tableheaders.FindElements(By.XPath("tr"));
IList<IWebElement> resultsrows = tableresults.FindElements(By.XPath("tr"));
And I do a for to get all the elements from each of the list:
IList<IWebElement> columnsTable;
foreach (IWebElement row in resultsrows)
{
columnsTable = row.FindElements(By.TagName("td"));
}
IDictionary<IWebElement, IList<IWebElement>> dict = new Dictionary<IWebElement, IList<IWebElement>>();
int index = 0;
foreach (var header in headersRows.SelectMany(r => r.FindElements(By.XPath("th"))))
{
dict.Add(header, resultsRows.Select(r => r.FindElements(By.TagName("td"))[index]).ToList());
index++;
}
This will give you a dictionary where the key is the th element, and the values are a list of td elements containing the values for the column.

Add two list<> in datarows c#

I am trying to add rows in the datarow one by one..For example first row is of list and second row is of list,then the third row is of list and fourth of list and so on..Here is my code ..
// Declaring a list of datahandling type data which contains
// groupid,stringid,stringtext etc.
List<Data> data = new List<Data>();
List<Data> diff = new List<Data>();
// Function which separates the relevant data from the string
// and stores it in the list.
control.Stringhandler(readcontents.Contents, ref data);
control.Stringhandler(readcontents.Translated_contents, ref diff);
foreach (var array in data)
{
datarows.Rows.Add(array.GroupID, array.StringID, array.StringText);
// datarows.Rows.Add(array.GroupID, array.StringID, array.StringText);
save = array.Lines + 1;
}
My question is that the foreach() adds data in the datarow,row by row ..I want to add diff row next to data row..
For example the datarow should be added in this way
datarow.row[0]=data;
datarow.row[1]=diff;
datarow.row[2]=data;
datarow.row[3]=diff;
That is what i am trying to do..
If both list are of <Data> type, you can concat first two list and then add rows. Like this
var lstCombined = data.Concat(diff)
foreach (var array in lstCombined )
{
datarows.Rows.Add(array.GroupID, array.StringID, array.StringText);
// datarows.Rows.Add(array.GroupID, array.StringID, array.StringText);
save = array.Lines + 1;
}

How to add a Arraylist in to a specific column of DataTable?

How to add a Arraylist in to a specific column of DataTable, for example, add Arraylist of name 'mylist' to the 5th column of the DataTable name mydatatable.
I try this code:
foreach(string item in mylist)
{
mydatatable.Rows.Add(item);
}
But when i am using this code, the data always fill into first column of the table, and when I am adding column index or column name i am getting error message. So anyone have any idea about it? And Thanks in advance for your time.
object[] fields = new object[5];
foreach(string item in mylist)
{
fields[4] = item;
mydatatable.Rows.Add(fields);
}

Categories