This is my code:
sql = "SELECT * FROM stock";
cmd = new SQLiteCommand(sql, mdb);
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
var_depot = reader["depot"].ToString();
ajouterlistview1 = new ListViewItem(reader["id"].ToString());
ajouterlistview1.SubItems.Add(reader["article"].ToString());
listView1.Items.Add(ajouterlistview1);
listView1.Refresh();
for (int i = 0; i < listView1.Columns.Count; i++)
{
if (var_depot == listView1.Columns[i].Text)
{
for (int j = 0; j < listView1.Items.Count; j++)
{
/*if (listView1.Columns[i].Text == var_depot )
{*/
listView1.Items[j].SubItems[1].Text = reader["quantite"].ToString();
//}
}
}
}
}
This code work fine but when I change this line:
listView1.Items[j].SubItems[1].Text = reader["quantite"].ToString();
to this line :
listView1.Items[j].SubItems[i].Text = reader["quantite"].ToString();
I get a error because my row X is not defined :/
What should I do please?
It looks like you have more listView1.Columns than listView1.Items[...].SubItems. Add some empty subItems (ajouterlistview1.SubItems.Add("")) to make sure that you can have the access to listView1.Items[...].SubItems[i].Text.
Try this way:
for (int i = 0; i < listView1.Columns.Count; i++)
{
if (var_depot == listView1.Columns[i].Text)
{
ajouterlistview1.SubItems.Add(reader["quantite"].ToString());
break;
}
else
ajouterlistview1.SubItems.Add("");
}
Related
I'm coding like below but it works incorrect.It perform(plus and delete) only 2->3 rows if data has 5->6 duplicate data.
Update and It works
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[1].Value.ToString() + Row.Cells[2].Value.ToString().ToUpper();
// MessageBox.Show(abc);
for (int j = i + 1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[1].Value.ToString() + Row2.Cells[2].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[5].Value = Convert.ToDouble(Row.Cells[5].Value.ToString()) + Convert.ToDouble(Row2.Cells[5].Value.ToString());
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
This should do the trick for you:
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[0].Value.ToString() + Row.Cells[1].Value.ToString().ToUpper();
for (int j = i+1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[0].Value.ToString() + Row2.Cells[1].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[2].Value = (int)Row.Cells[2].Value + (int)Row2.Cells[2].Value;
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
You basically need to keep track of j variable as you remove rows from the collection.
If you're a fan of LINQ and don't mind a bit of a convoluted code, here is another approach:
for (int i = 0; i < dataGridView1.RowCount; i++) //compare data
{
var R = dataGridView1.Rows[i];
var V = R.Cells[0].Value.ToString() + R.Cells[1].Value.ToString().ToUpper();
var DupRows = dataGridView1.Rows.Cast<DataGridViewRow>().Skip(i + 1).
Where(r => r.Cells[0].Value.ToString() + r.Cells[1].Value.ToString().ToUpper() == V);
R.Cells[2].Value = (int)R.Cells[2].Value + DupRows.Sum(r => (int)r.Cells[2].Value);
foreach (var DupRow in DupRows)
DupRow.Tag = "Del";
}
for (int i = 0; i < dataGridView1.RowCount; i++)
{
var R = dataGridView1.Rows[i];
if (R.Tag?.ToString() == "Del")
{
dataGridView1.Rows.Remove(R);
i--;
}
}
As a word of advice, this kind of stuff is handled far more easily in the back-end. Whatever your DataGridView is bound to, be it a DataTable or a generic collection, you should implement duplicate removal there instead of directly playing with DataGridView cells.
Related to this question : I'm trying to write an "INSERT" loop : I have got the following query:
CommandText = "INSERT into sample2Prot(timestp,idq,idz,prot,Lhowmany,Rhowmany) VALUES(#timestp,#idq,#idz,#prot,#Lhowmany,#Rhowmany)";
When I execute my code (which can be found just below) I get the following error:
'#timestp' cannot be handle by SqlParameterCollection. ("timestp" = tableNames[0], of string type)
for (int j = 0; j < tableNames.Count; j++)
// tableNames contains the name of the columns, tableTypes the types of the columns
// tableTypes contains
{
if (tableTypes[j] == "INTEGER")
{
myCommand3.Parameters.Add("#" + tableNames[j], System.Data.SqlDbType.Int);
Console.WriteLine("#" + tableNames[j]);
}
else
{
myCommand3.Parameters.Add("#" + tableNames[j], System.Data.SqlDbType.VarChar);
Console.WriteLine("#" + tableNames[j]);
}
}
Console.WriteLine(myCommand3.CommandText);
for (int f = 0; f < total.Count(); f++)
{
for (int k = 0; k < tableNames.Count; k++)
{
myCommand3.Parameters.Clear();
myCommand3.Parameters["#" + tableNames[k]].Value = total[f][k];
}
myCommand3.ExecuteNonQuery();
}
Has someone an idea ? Don't mind asking for more precision.
for (int f = 0; f < total.Count(); f++)
{
for (int k = 0; k < tableNames.Count; k++)
{
myCommand3.Parameters.Clear(); // < ==== PROBLEM is here!
// After clearing the Parameters, there is no Parameter "["#" + tableNames[k]]"
// hence "SqlParameter mit ParameterName '#timestp' ist nicht in SqlParameterCollection enthalten."
// (eng: SqlParameterCollection does not contain SqlParameter with name '#timestp' )
myCommand3.Parameters["#" + tableNames[k]].Value = total[f][k];
}
myCommand3.ExecuteNonQuery();
}
In above code, move the Clear() statement like so:
for (int f = 0; f < total.Count(); f++)
{
for (int k = 0; k < tableNames.Count; k++)
{
myCommand3.Parameters["#" + tableNames[k]].Value = total[f][k];
}
myCommand3.ExecuteNonQuery();
}
myCommand3.Parameters.Clear();
I am using a data grid, but the values do not display as I would like them to. My current code is below, how would I go about inverting the rows?
string[] strOutput = strLine.Split('_', ',','=');
int totalRows = Convert.ToInt16(strOutput[4]);
int totalCols = Convert.ToInt16(strOutput[5]);
int itemIndex = 8;
for (int i = 0; i < totalCols; i++){
dataGridView1.Columns.Add("Col", "Col");
}
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++) {
for (int j = 0; j < totalCols; j++) {
dataGridView1.Rows[i].Cells[j].Value = strOutput[itemIndex];
itemIndex += 2;
}
}
dataGridView1.Visible = true;
To invert i.e. reverse DataGridViewRows you can use this:
void ReverseDGVRows(DataGridView dgv)
{
List<DataGridViewRow> rows = new List<DataGridViewRow>();
rows.AddRange(dgv.Rows.Cast<DataGridViewRow>());
rows.Reverse();
dgv.Rows.Clear();
dgv.Rows.AddRange(rows.ToArray());
}
If you only need to do it once you could instead either:
loop over the lines of the source file in reverse
or instead of Adding the rows (to the end) Insert at the top:
dtnew.Rows.Insert(0, currentDataRowView.Row);
Inverting rows:
"DataGridView.Rows".- will give you "DataGridViewRowCollection"
Iterate the collection in reverse order and create a new datatable. (for loop from max size to zero)
Assign the new datatable to datagridview source.
This rough code written in note pad for your idea. I do not have IDE now.
DataGridViewRowCollection dgRowColllection = dataGridView1.Rows;
DataTable dtnew = new DataTable();
for(i = dataGridView1.RowCount; i < 1 ; i--)
{
DataRowView currentDataRowView = dgRowColllection[i].Row;
dtnew.Rows.Add(currentDataRowView.Row);
}
dataGridView1.source = dtnew;
Can't comment on Pavan's answer because I don't have 50 reputation, but are you getting the error because the loop should be something like:
int totalRows = Convert.ToInt16(strOutput[4]);
int totalCols = Convert.ToInt16(strOutput[5]);
int itemIndex = 8;
for (int i = 0; i < totalCols; i++)
{
dataGridView1.Columns.Add("Col", "Col");
}
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++)
{
for (int j = 0; j < totalCols; j++)
{
dataGridView1.Rows[i].Cells[j].Value = strOutput[itemIndex];
itemIndex += 2;
}
DataGridViewRowCollection dgRowColllection = dataGridView1.Rows;
DataTable dtnew = new DataTable();
for (i = dataGridView1.Items.Count; i < 1; i--)
{
DataRowView currentDataRowView = dgRowColllection[i].Row;
dtnew.Rows.Add(currentDataRowView.Row);
}
dataGridView1.DataSource = dtnew;
}
dataGridView1.Visible = true;
}
if (cmd == "card_request") {
Debug.Log("FOund cards");
ISFSObject responseParams = (SFSObject)evt.Params["params"];
Debug.Log(responseParams.GetClass("cards").ToString());
SFSArray data = (SFSArray)responseParams.GetSFSArray("cards");
Debug.Log(data.GetUtfString(0));
//for (int i = 0; i < data.GetUtfString(0).IndexOf("value"); i++) {
firstSplit = data.GetUtfString(0).Split(';');
Debug.Log(firstSplit);
//}
for (int i = 0; i < firstSplit[0].IndexOf("value"); i++) {
secondSplit = firstSplit[0].Split(':');
Debug.Log(secondSplit);
}
for (int i = 0; i < secondSplit[0].IndexOf("value"); i++) {
thirdSplit = secondSplit[0].Split(',');
Debug.Log(thirdSplit);
}
}
the data is coming fine at this line Debug.Log(data.GetUtfString(0)); But when I try to split it it gives errors. Somebody can please suggest me the effective way to split UTF string. Null exceptions occurs at secondSplit and thirdSplit
As the title suggests, I am trying to generate a procedural hex grid in Unity using C#.
void Start ()
{
//5 mid
startRowSize = 5;
for (int i = 0; i < startRowSize; i++)
{
GameObject newHex = (GameObject)Instantiate(hex);
hexWidth = newHex.gameObject.renderer.bounds.size.z;
newHex.transform.Rotate(new Vector3(0,30,0));
newHex.transform.position = new Vector3((i * hexWidth),0,0);
}
for (int row = 0; row <= startRowSize-1; row ++)
{
for (int i = 0; i < row; i++)
{
GameObject newHex = (GameObject)Instantiate(hex);
newHex.transform.Rotate(new Vector3(0,30,0));
newHex.transform.position = new Vector3(((i*hexWidth)+((hexWidth/2))+(row*(hexWidth/2))),0,((startRowSize-row))*-(hexWidth/1.17f));
}
}
}
The code works, however the rows are being generated "backwards", meaning the outer rows contain the higher amounts of hexes, while the inner rows contain the smallest.
This is obviously the opposite effect that I am trying to achieve. I've been messing with this code for hours and I can't figure out why.
Any thoughts?
So after a few more hours of messing around with the code, I figured out why it wasn't iterating properly. He's the refined code...
void Start ()
{
//5 mid
startRowSize = 10;
for (int i = 0; i < startRowSize; i++)
{
GameObject newHex = (GameObject)Instantiate(hex);
hexWidth = newHex.gameObject.renderer.bounds.size.z;
newHex.transform.Rotate(new Vector3(0,30,0));
newHex.transform.position = new Vector3((i * hexWidth),0,0);
}
for (int row = 0; row < startRowSize; row++)
{
for (int i = 0; i < startRowSize-1-row; i++)
{
GameObject newHex = (GameObject)Instantiate(hex);
newHex.transform.Rotate(new Vector3(0,30,0));
if (row == 0)
{
newHex.transform.position = new Vector3(((i*hexWidth)+(hexWidth/2)),0,-(hexWidth/1.17f));
}
else
{
newHex.transform.position = new Vector3((i*hexWidth)+((row+1)*(hexWidth/2)),0,(row+1)*-(hexWidth/1.17f));
}
}
}
for (int row = 0; row < startRowSize; row++)
{
for (int i = 0; i < startRowSize-1-row; i++)
{
GameObject newHex = (GameObject)Instantiate(hex);
newHex.transform.Rotate(new Vector3(0,30,0));
if (row == 0)
{
newHex.transform.position = new Vector3(((i*hexWidth)+(hexWidth/2)),0,(hexWidth/1.17f));
}
else
{
newHex.transform.position = new Vector3((i*hexWidth)+((row+1)*(hexWidth/2)),0,(row+1)*(hexWidth/1.17f));
}
}
}
}
Now, can anyone suggest how to clean it up a bit? My brain is fizzled...
Just a tip, Awake happens before Start().
void Awake()
{
//5 mid
startRowSize = 10;
for (int i = 0; i < startRowSize; i++)
{
GameObject newHex = HexCreator();
hexWidth = newHex.gameObject.renderer.bounds.size.z;
newHex.transform.position = new Vector3(i * hexWidth, 0, 0);
}
for (int row = 0; row < startRowSize; row++)
for (int i = 0; i < startRowSize-1-row; i++)
{
GameObject newHex = HexCreator();
if (row == 0)
newHex.transform.position = new Vector3(i*hexWidth + hexWidth/2, 0, -(hexWidth/1.17f));
else
newHex.transform.position = new Vector3(i*hexWidth + ((row+1)*(hexWidth/2)), 0,(row+1)*-(hexWidth/1.17f));
}
for (int row = 0; row < startRowSize; row++)
for (int i = 0; i < startRowSize-1-row; i++)
{
GameObject newHex = HexCreator();
if (row == 0)
newHex.transform.position = new Vector3(i*hexWidth+ hexWidth/2, 0, hexWidth/1.17f);
else
newHex.transform.position = new Vector3(i*hexWidth + ((row+1)*(hexWidth/2)), 0, (row+1)*(hexWidth/1.17f));
}
}
Since you have this code repeating I made it into a function. That way if you need to upgrade it all you do is go to the function and change it there.
private GameObject HexCreator()
{
GameObject newHex = (GameObject)Instantiate(hex);
newHex.transform.Rotate(new Vector3(0,30,0));
return newHex;
}