I have a column which is my third column.
I am stuggling to place the values into an unknown array size when it the button is clicked.
int rowCount = dataGridView1.Rows.Count;
string[] Priority = new string[rowCount];
if (rowCount > 1) // only sort if bigger than one line
{
for (int i = 2; i < rowCount; i++)
{
dataGridView1.Rows[i].Cells[2].Value = priority[i]; //put data into row thats been added
}
}
I get a System.IndexOutOfRangeException when I try with more than one row, it is fine if I want to add one value to an array and Im not sure what is wrong with it? Thanks
Try this:
// Modify the value in the first cell of the second row.
this.dataGridView1.Rows[i].Cells[0].Value = "new value";
// The previous line is equivalent to the following line.
this.dataGridView1[i, 2].Value = "new value";
Not sure if this is your problem, but you instantiate as "Priority"
then you are using 'priority'
prehaps a property named elsewhere?
I agree with the off by one. the i = 2 is confusing
I'm new, but I thought arrays were always finite in size.
just my thoughts.
Related
Ok I am stumped the set font works and sets the correct cell. The Fill.BackgroundColor.SetColor does not work. It literally does nothing. Am I missing something? Every search I do that syntax should work.
Yes this is reading a data table dt that is not the issue the loop works properly.
int startupRow = 2; // row 1 is header
for (int row = startupRow; row <= dt.Rows.Count; row++)
{
//if allocation check is populated it will have a value > 0
if (Convert.ToInt32(workSheet.Cells[row, 8].Value) > 0)
{
//if Balance Remaining Barrels < allocation check
if (Convert.ToInt32(workSheet.Cells[row, 7].Value) < Convert.ToInt32(workSheet.Cells[row, 8].Value))
{
//set the font to red
var cell = workSheet.Cells[row, 7];
cell.Style.Font.Color.SetColor(System.Drawing.Color.Red);
cell.Style.Font.Bold = true;
//Setting the background color to red
cell.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
}
}
}
I resolved my own question so hopefully this helps someone else.
The border has to be last in the formatting. Certain elements take precedent over other elements and they will disappear.
when i creat unlimited textbox in gridview dynamically how can i access them?
for example:
int uste_uzaklik = 30;
int nesne = ListBox1.Items.Count;
Array.Resize(ref textboxarray, nesne * nesne);
for (int str = 0; str < nesne; str++)
{
for (int stn = 0; stn < nesne; stn++)
{
textboxarray[idm] = new TextBox();
textboxarray[idm].Font.Bold = true;
textboxarray[idm].Font.Name = "Verdana";
textboxarray[idm].ID = idm.ToString();
textboxarray[idm].ToolTip = textboxarray[idm].ID;
GridView2.Rows[str].Cells[stn + 1].Controls.Add(textboxarray[idm]);
if (str == stn) textboxarray[idm].Enabled = false;
uste_uzaklik += 30;
idm++;
}
}
i add texboxes in gridview...you can imagine a matris...
there is no problem...
but when i access them like this:
if (((TextBox)(GridView2.Rows[str].Cells[stn].FindControl(idm.ToString()))).Text != null)
{
matris[i, j] = Convert.ToInt32(GridView2.Rows[str].Cells[stn].Text);
}
occur an error
Object reference not set to an instance of an object.
how can i solve this problem?
References you have to controls don't cease to exist you add them to another control. You've already created an array of your TextBoxes, and you should use that to access them instead of trying to dig into the GridView in which you've added them every single time you want to change them.
Granted, you're going from a one-dimensional array of TextBoxes to a two-dimensional layout within the GridView, so you'll either have to find some way to establish how the indices match up between the two. Or, more easily, you could just turn textboxarray into a two-dimensional array and just have it exactly match the way it's laid out in the GridView. Either way, I think it'll be a lot less work than having to muck around in the GridView.
I want to remove every column after the 3rd column from a CSV file loaded into a datatable, but I'm getting odd results. Here's my code.
System.Data.DataTable csv_datatable = null;
using (System.IO.StreamReader re = new System.IO.StreamReader(model.file.InputStream))
{
csv_datatable = CsvParser.Parse(re as System.IO.TextReader);
for (int x = 3; x < csv_datatable.Columns.Count + 1; x++)
{
csv_datatable.Columns.RemoveAt(x);
}
}
My sample CSV file:
7 columns, and I want to keep the first three.
email,first,last,middle,nick,job,phone
roryok#fakedomain.com,rory,wally,ok,danger,developer,555-0123
This is the result I get.
email,first,last,nick,phone
roryok#fakedomain.com,rory,ok,danger,555-0123
As you can see, rather than removing the last columns as I would expect, the code actually removes the 4th and 6th columns.
As usual, figured this out as I was posting, but I'll post the solution anyway in case it helps someone.
As each column is removed, the index for each column changes. So when you remove column 3, 4 becomes the new 3. This code works:
for (int x = 0; x < csv_datatable.Columns.Count; x++)
{
csv_datatable.Columns.RemoveAt(3);
}
This will loop over the number of columns, and remove the third column over and over again until everything is gone.
I need to iterate over a specific excel row. For now I've got a code to iterate over a column and I want it to be similar to that. It looks like this:
int columnLength = xlWorkSheet.UsedRange.Rows.Count;
string lastCell = Regex.Replace(CONST_FIRST_CELL, #"\d+", columnLength.ToString()); //will give me the last cell in the column
var excelColumn = xlWorkSheet.Range[CONST_FIRST_CELL, lastCell ];
if (excelColumn == null)
{
throw new Exception("bad col");
}
for (int i = 1; i < columnLength ; i++)
{
Excel.Range currentValue = excelColumn [i];
....DO SOME STUFF....
}
how can I iterate over a specific row? I'm not sure how to get the last column like I got the last row cell in the above implementation since then I just had to switch a number with the length but now I somehow need to get the correct last cell of a row (which means switching the letters somehow i.e C4 to AD4) in order to get the range of first cell row and last...
The best solution I guess involves a foreach loop somehow?
You were almost there, your loop just needs some tuning:
//Input all the Int values you want
int targetRow = 1;
int startCol = 1;
int maxCol = 10; //With this value the loop below will iterate until column 9 (inclusive)
for (int i = startCol; i < maxCol ; i++)
{
Excel.Range currentRange = (Excel.Range)xlWorkSheet.Cells[targetRow, i];
if (currentRange.Value2 != null)
{
string curVal = currentRange.Value2.ToString();
}
}
IMO this is the best way to iterate through cells (by considering rows and/or columns). You can do it differently (on the lines of what you were trying): iterating within the columns of a given range (you would need to define the range as Excel.Range Type and then rely on the in-built property Columns), although I think that this can be more confusing. Example: if you have as input range C1:H5, "C:C" is the first column, "D:D" the second column, etc. With my approach, the first column will always be "A:A", the second column "B:B", etc.
Example of iterating through columns in a given range (inputRange):
foreach(Excel.Range curCol in inputRange.Columns)
{
if (curCol.Value2 != null)
{
//As far as each column only has one row, each column can be associated with a cell
string curVal = curCol.Value2.ToString();
}
}
i have an argument outofrangeexception for my datagridview.
i try to fill it.
It stops when i = 1;
I dont know where is my mistake but its not the array donnee[,]
here's my code
for(int i = 0; i < donnee.Length/4; i++){
dataGridView1.Rows[i].Cells[0].Value = donnee[i,0];
dataGridView1.Rows[i].Cells[1].Value = donnee[i,1];
dataGridView1.Rows[i].Cells[2].Value = donnee[i,2];
dataGridView1.Rows[i].Cells[3].Value = donnee[i,3];
}//REMPLIR DATAGRIDVIEW
Thanks
If you are filling it, you need to add rows as you go. I expect that you are currently filling the default "new data" row (as zero), but you should really be allocating your own each time, simply via .Rows.Add(). You could do this per-row, or via dataGridView1.Rows.Add(donnee.Length/4); before the loop.