Populate datagridview from database - c#

I am trying to fill datagridview from database (cassandra), but for some reason it shows only one row instead of two, can you tell me what I am doing wrong.
public void show_db()
{
var cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
var session = cluster.Connect("hotel");
RowSet rs = session.Execute("SELECT * FROM hotel.staff");
foreach (var res in rs)
{
for (int i = 0; 2 < 3; i++)
{
for (int j = 0; j < 4; j++)
{
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("staff_id");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("username");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("password");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("name");
}
}
}
}
It's from database
This is from the application

Related

Vlookup Multiple Criteria C#

I have two datagridviews:
datagridView1, with col A and B (Cells[0], Cells1):
datagridView2 (Cells[0], Cells[1], Cells[2]):
I would like to compare the value in between two tables and if:
- the value in datagridView1.rows[i].Cells[0].value = datagridView2.rows[j].Cells[0].value AND the value in datagridView1.rows[i].Cells[1].value = datagridView2.rows[j].Cells[1].value THEN write in datagridView1.rows[i].Cells[2].value the value from the third column in datagridView2:
I believe you are looking for something like this. It would depend on how you are populating your DataGridViews. There is also no error handling which should be implemented.
for (var i = 0; i < dataGridView1.Rows.Count; i++)
{
var r1 = dataGridView1.Rows[i];
var r2 = dataGridView2.Rows[i];
if (r1.Cells[0].Value == r2.Cells[0] && r1.Cells[1].Value == r2.Cells[1])
r1.Cells[2].Value = r2.Cells[2].Value;
}
Here is the second loop I've added to match any row and works fine:
for (var i = 0; i < src1.Rows.Count; i++)
{
var r1 = src1.Rows[i];
for (var j = 1; j < src2.Rows.Count; j++)
{
var r2 = src2.Rows[j];
if (r1[0].Equals(r2[0]) && r1[1].Equals(r2[1]))
{
r1[2] = r2[2];
}
}
}

Converting this MatLab iterating code to C#. Outputs from matlab and c# are different

I am trying to convert this code:
function [C] = cumulativeMaxV2(A)
cols = size(A,2);
bscans = size(A,3);
C = zeros(size(A));
for col = 1:cols
for bscan = 1:bscans
aline = A(:,col,bscan);
for i = 1:length(aline)
if i == 1
C(i,col,bscan)=0;
else
C(i,col,bscan) = max(A(1:i-1, col,bscan));
end
end
end
end
My C# code is below:
static double[,,] CumulativeMax(double[,,] A)
{
int cols = 304; //A.GetLength(1);
int bscans = 304; //A.GetLength(2);
double[,,] C = new double[160, 304, 304];
Console.Write("Processing... ");
using (var progress = new ProgressBar())
{
for (int col = 0; col < cols; col++)
{
for (int bscan = 0; bscan < bscans; bscan++)
{
double[] aline = new double[160];
for (int i = 0; i < 160; i++)
aline[i] = A[i,col,bscan];
for (int i = 0; i < aline.GetLength(0); i++)
{
if (i == 0)
C[i,col,bscan] = 0d;
else if (i == 1)
{
double[] temp = new double[i];
for (int x = 0; x < i; x++)
temp[x] = A[x,col,bscan];
C[i,col,bscan] = temp.Max();
}
else
{
double[] temp = new double[i - 1];
for (int x = 0; x < i - 1; x++)
temp[x] = A[x,col,bscan];
C[i,col,bscan] = temp.Max();
}
}
}
progress.Report((double)col/cols);
}
}
Console.WriteLine("Done.");
return C;
}
Outputs from MatLab do not match those from the C# code.
Any pointers to where the bugs are in my C# code would be great. I'm not very good with MatLab.
I think this may be due to how MatLab's max function deals with infinity and NaNs.

How to insert into a nested list

I initialize a list of list:
List<List<double>> List = new List<List<double>>();
for(int k = 0; k < 4; k++)
{
liste_moyenne.Add(new List<double>(5));
}
I want to insert the values of a list (this list is generated by another function) like this:
for that I did this piece of code:
for (int i = 0; i < List.Count; i++)
{
for (int j = 0; j < myList.Count; j++)
{
List[i].Add(AddSmthToWork(myList[j]))
}
}
How can I do that?
Here's an example using the AddRange method:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var listOfList = new List<List<double>>();
for (var i = 0; i < 4; i++)
{
listOfList.Add(new List<double>(5));
}
foreach (var item in listOfList)
{
item.AddRange(GetList(5));
}
for(var i = 0; i < listOfList.Count; i++)
{
for (var j = 0; j < listOfList[i].Count; j++)
{
Console.WriteLine($"listOfList[{i}][{j}] = {listOfList[i][j]}");
}
}
}
private static IList<double> GetList(int maxCapacity)
{
var newList = new List<double>(maxCapacity);
for (var i = 0; i < maxCapacity; i++)
{
newList.Add(i);
}
return newList;
}
}
With the output:
listOfList[0][0] = 0
listOfList[0][1] = 1
listOfList[0][2] = 2
listOfList[0][3] = 3
listOfList[0][4] = 4
listOfList[1][0] = 0
listOfList[1][1] = 1
listOfList[1][2] = 2
listOfList[1][3] = 3
listOfList[1][4] = 4
listOfList[2][0] = 0
listOfList[2][1] = 1
listOfList[2][2] = 2
listOfList[2][3] = 3
listOfList[2][4] = 4
listOfList[3][0] = 0
listOfList[3][1] = 1
listOfList[3][2] = 2
listOfList[3][3] = 3
listOfList[3][4] = 4
See this tutorial
List<List<double>> nestedList = new List<List<double>>();
for (int i = 0; i < 4/*myCount*/; i++)
{
List<double> sublist = new List<double>();
for (int j = 0; j < 4/*myCount*/; j++)
{
sublist.Add(/*myValue*/)
}
nestedList.Add(sublist);
}

How to get entire no of rows present in data table into single label using button click event in window form c#

How to get entire rows present in data table into single label(for every row we should not take new label,we should take only single label for "N" no of rows present in datatable) using button click event in window form c#
for (int i = 1, r = 0; i <= dt.Rows.Count; i++, r++)
{
label19.Text = Convert.ToInt32(i).ToString();
label20.Text = dt.Rows[r]["Ques"].ToString();
}
For All Rows and columns
line.Text = "";
for(int i = 0; i<dt.Rows.Count; i++)
{
for(int j = 0; j<dt.Columns.Count; j++)
{
line.Text +=dt.Rows[i][j].ToString()+" ";
}
line.Text +=Environment.NewLine;
}
this should work
for your example
for(int i = 0; i<dt.Rows.Count; i++)
{
line.Text +=(i+1).ToString()+" "+dt.Rows[i]["Ques"].ToString()+" ";
line.Text +=Environment.NewLine;
}
you can format it To Look Porper in web project
line.Text ="<table>";
for(int i = 0; i<dt.Rows.Count; i++)
{
line.Text ="<tr>";
for(int j = 0; j<dt.Columns.Count; j++)
{
line.Text +="<td>"+dt.Rows[i][j].ToString()+"</td>";
}
line.Text +="</tr>";
}
line.Text +="</table>";

Programmatically adding list to datagridview

I have procedure that programmatically adding list to datagridview. For Example:
public List<Color_INFO> addrowtocolors()
{
List<Color_INFO> result = dal.GetColor();
for (int i = 0; i < list.Count; i++)
{
var index = grdColors.Rows.Add();
grdColors.Rows[index].Cells["Code"].Value = result[i].Code.ToString();
grdColors.Rows[index].Cells["Desc"].Value = result[i].Desc.ToString();
}
return null;
}
But when I call it is adding 3 same rows to datagridview , and in list i have only one.
I know that I can use dataset option , but that not fit for my needs.
Thanx.
As per my comment... I've also removed the return value as it seemed strange to just return null. (or even the proposed list based on your function name).
public void addrowtocolors()
{
for (int i = 0; i < result.Count; i++)
{
var index = grdColors.Rows.Add();
grdColors.Rows[index].Cells["Code"].Value = result[i].Code.ToString();
grdColors.Rows[index].Cells["Desc"].Value = result[i].Desc.ToString();
}
}
or
public void addrowtocolors()
{
for (int i = 0; i < list.Count; i++)
{
var index = grdColors.Rows.Add();
grdColors.Rows[index].Cells["Code"].Value = list[i].Code.ToString();
grdColors.Rows[index].Cells["Desc"].Value = list[i].Desc.ToString();
}
}

Categories

Resources