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.
Related
Not sure if this is the best way to do this but I've created a 2D array from values in an excel file that i want to use as variables within my application that control where to look for things like file paths and files.
The array is built and all data from the file is contained within it:
for (int i = 0; i <= bindingSourceConfig.Count - 1; i++) // for each row in the binding source data
{
for (int j = 0; j <= 2 - 1; j++) // for each column, only need one and 2
{
System_Var_Array[i, j] = (bindingSourceConfig.DataSource as DataTable).Rows[i][j].ToString();
}
}
Now i want to be able to look in the array for my variable say "Project_Directory" and have it return "C:\Users\User\Dropbox\default\master\support"
Is this even possible?
EDIT 1:
Purpose of doing it this way is to make an easily customizable/configurable multi project environment where by anyone can simply edit the paths in the excel file and import that file into the application.
I can not in my incompetence see a way of setting these 'variables' at a class level without first extracting the 'variable' and the 'value' from the excel data.
Is there a simple way of looking up the 'variable' in bindingSource?
EDIT 2:
debugger image
var name = "Project_Directory";
string value = null;
for (int i = 0; i<System_Var_Array.GetLength(0); i++) {
if (System_Var_Array[i,0]==name) {
value = System_Var_Array[i,1];
break;
}
}
After the loop stop, value will contain the data you need, assuming it exists (otherwise it will be null. A far simpler approach will be using Dictionary<string,string>, if you generate it with
var systemVars = new Dictionary<string,string>();
var dt = bindingSourceConfig.DataSource as DataTable;
for (int i = 0; i < bindingSourceConfig.Count; i++) // for each row in the binding source data
{
systemVars[dt.Rows[i][0].ToString()] = dt.Rows[i][1].ToString();
}
then to get the value for "Project_Directory", simply call
var value = systemVars["Project_Directory"];
You should use an appsettings file for this instead, it's the dotnet standard solution for this sort of thing. Please see https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration.
i am trying to put the values of a certain column from a dataGrid to a string hence
i am getting this error like "An unhandled exception of type 'System.NullReferenceException' ".
What i am trying to do is to put that collection of data to a collection that will suggest what in it in a textbox.
heres my code; I hope there will be someone who could help me.
just a newbee here, sorry i might look stupid for this query [pizz]
foreach (DataGridViewRow row in dataGridView1.Rows)
{
xxxx[row.Index] = row.Cells[3].Value.ToString().Trim();
}
for (int x = 10; x <= dataGridView1.RowCount - 1; x++)
{
collection.Add(xxxx[x].ToString());
}
Search.AutoCompleteCustomSource = collection;
Search.AutoCompleteMode = AutoCompleteMode.Suggest;
Search.AutoCompleteSource = AutoCompleteSource.CustomSource;
i even tried this method on getting values from the column;
for (int x = 1; x <= dataGridView1.RowCount - 1; x++)
{
xxxx[x] = dataGridView1.Rows[x].Cells[searchChokoy].Value.ToString();
}
i already figured out what's the reason why i could not get the data from the datagrid it just that i have to put -2 on the condition on forloops because the source file is a csv file tho and indexing is quite a bit different.
Anyways i just tried my luck here, Thanks!
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.
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'm currently coding a project that can take up to 200 entries of a specific product, as determined by user input. Basically, my GUI loads, and I use jQuery to dynamically build the entries whenever there is a change to the amount field. When using jQuery, I simply give each of them ids in the form of variable1, variable2, ...., variableX (where X is the amount of entries indicated). Small snippet of code to clarify:
for(var i = 1;i <= amount_selected; i++) {
$('table_name tr:last').after('<tr><td><input type="text" id="variable' + i + '"></td></tr>');
}
Now when I try to move to the back end, I'm trying to reference these variable names by putting them in a list. I went ahead and put them in a list of HtmlInputText, to call the Variable names from the list itself. (This would save having to call all (up to 200) methods manually, which is really not an option).
So what I did (in C#) was:
List<HtmlInputText> listvar = new List<HtmlInputText>();
for(int i = 1; i <= amount_selected; i++) {
string j = "variable" + Convert.ToString(i);
HtmlInputText x = j;
listvar.Add((x));
samplemethod(listvar[i]);
}
But it's not working at all. Does anyone have any ideas as to how this would be done, without doing so manually? I know my logic might be completely off, but hopefully this illustrates at least what I'm attempting to do.
I'm assuming these inputs are in a form? If you're submitting then you can access the text boxes from the Request object:
List<string> results = new List<string>();
for (int i = 1; i <= amount_selected; i++)
{
string s = String.Format("{0}", Request.Form["variable" + Convert.ToString(i)]);
results.Add(s);
}
you could do $("#variable" + Convert.ToString(i)).val()