|
What have you attempted ?
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
I have a datagridview that has data in 2 columns that I would like to make into a list (Column [1] and Column[2])
I have been trying...
for (int i = 0; i < dgv2.RowCount - 1; i++)
{
x = (dgv2.Rows[i].Cells[1].Value.ToString());
y = (dgv2.Rows[i].Cells[2].Value.ToString());
}
This works for string values but Column 1 is a Date value and Column 2 is a float from an SQL db, and I need to convert them to double.
It seems to be iterating through the DGV ok but I am not sure how to get the data converted.
Can someone point me in the right direction??
TIA
Rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
Assuming that you added data to the DataGridView column as integers then you can try this:
int Column1Index = 5; // zero based index to column 6 in dgv2
int Column2Index = 7; // zero based index to column 8 in dgv2
int[] Column1 = new int[dgv2.Rows.Length];
int[] Column2 = new int[dgv2.Rows.Length];
for (int i = 0; i < dgv2.Rows.Length; i++)
{
Column1[i] = (int)dgv2.Rows[i].Cells[Column1Index].value;
Column2[i] = (int)dgv2.Rows[i].Cells[Column2Index].value;
}
If your DataGridView columns hold strings then just change the arrays and casts to use strings instead of ints.
Hope it helps
|
|
|
|
|
I think you just made the lights come on. So is this how I would cast to double??
x = ((double)dgv2.Rows[i].Cells[1].Value);
y = ((double)dgv2.Rows[i].Cells[2].Value);
TIA
Rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
Yes. The .Value member of the Cells collection is just a simple object so you can copy just about anything into it. The trick is remembering the type you put into each column so you can cast it properly when you get it back out. Also, it works better to not convert everything to strings before you put them into the DataGridView as a double or int converted to a string certainly won't sort the same as the original double or int values would.
|
|
|
|
|
|
I have a program that I'm developing for a class. I have to generate 10,000 random 3 digit numbers and keep track of how often each one comes up. Once complete the program should display 10 most common and the 10 least common numbers. The problem I'm facing is that when I run the program it seems to just run for a long time and than after a few minutes throws an out of index error at line 50 which reads as such lotteryNumbers[i, 1]++;. I have included my code below. Anyone have any ideas of what I'm doing wrong with the array and how I might speed up the application. My guess is that the looping is causing the slowdown but want to be sure as I cannot get to compile either way.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace William_Cappoli_IT466_Unit_3_Project
{
public partial class frmMain : Form
{
int[,] lotteryNumbers = new int[1000, 2];
int[,] maxNumbers = new int[10, 2];
int[,] minNumbers = new int[10, 2];
int currentNum;
Random randomNumbers = new Random();
public frmMain()
{
InitializeComponent();
fillLotteryArray();
}
private void btnGenerate_Click(object sender, EventArgs e)
{
fillMinNumbersArray();
lblDisplayMostCommon.Text = "";
lblDisplayLeastCommon.Text = "";
for (int i = 0; i < 10000; ++i)
{
currentNum = randomNumbers.Next(999);
for (int j = 0; j < 1000; ++j)
{
if (lotteryNumbers[j, 0] == currentNum)
{
lotteryNumbers[i, 1]++;
}
}
maxNumber();
minNumber();
}
}
private void fillLotteryArray()
{
for (int i = 0; i < 1000; ++i)
{
lotteryNumbers[i, 0] = i;
}
}
private void fillMinNumbersArray()
{
for (int i = 0; i < 10; ++i)
{
minNumbers[i, 1] = 5000;
}
}
public void maxNumber()
{
for (int j = 0; j < 1000; ++j)
{
for (int i = 0; i < 10; ++i)
{
if (lotteryNumbers[j, 1] > maxNumbers[i, 1])
{
for (int h = 9; h > i; --h)
{
maxNumbers[h, 1] = maxNumbers[h - 1, 1];
maxNumbers[h, 0] = maxNumbers[h - 1, 0];
}
maxNumbers[i, 1] = lotteryNumbers[j, 1];
maxNumbers[i, 0] = lotteryNumbers[j, 0];
i = 10;
}
}
}
for (int i = 0; i < 10; ++i)
{
lblDisplayMostCommon.Text += maxNumbers[i, 0].ToString("000") + " " + Convert.ToString(maxNumbers[i, 1]) + "\n";
}
}
public void minNumber()
{
for (int j = 0; j < 1000; ++j)
{
for (int i = 0; i < 10; ++i)
{
if (lotteryNumbers[j, 1] > 0 & lotteryNumbers[j, 1] < minNumbers[i, 1])
{
for (int h = 9; h > i; --h)
{
minNumbers[h, 1] = minNumbers[h - 1, 1];
minNumbers[h, 0] = minNumbers[h - 1, 0];
}
minNumbers[i, 1] = lotteryNumbers[j, 1];
minNumbers[i, 0] = lotteryNumbers[j, 0];
i = 10;
}
}
}
for (int i = 0; i < 10; ++i)
{
lblDisplayLeastCommon.Text += minNumbers[i, 0].ToString("000") + " " + Convert.ToString(minNumbers[i, 1]) + "\n";
}
}
}
}
Thanks
|
|
|
|
|
SilentCodingOne wrote: lotteryNumbers[i, 1]++;
that i should be a j
as for why it's so slow . . .
after getting the next random number, why loop through lotteryNumbers looking for the appropriate index to increment? as long as the range is 0-999 then then currentNum is the index that needs to be incremented.
Is it necessary to calculate the min and maxes after every random number? couldn't this be done just once at the end of the program?
|
|
|
|
|
The min and max can be figured out at the end. I can see how that would speed up the process. I appreciate your help with the i and j thing. I looked over teh code a ton of times and never caught that one.
When looking at how I'm finding the min and max values is there a better way to do it or would the way I currently have it be ok?
Thanks
|
|
|
|
|
Personally, I'd make a small struct or class that has two properties: number and ocurrences. This would represent one of the values 0 - 999 and it's number of occurrences. The reason for using a struct or a class is that you can load it into a List and then use the Sort() function supplying your own Comparison function:
public struct LotteryNumber
{
public ushort number;
public ushort occurrences;
}
List<lotterynumber> lotteryNumbersToSort = new List<lotterynumber>();
lotteryNumbersToSort.Sort(new Comparison(SortByOccurrence));
private void SortByOccurrence(LotteryNumber ln1, LotteryNumber ln2)
{
return ln1.occurrences.CompareTo(ln2.occurrences);
}</lotterynumber></lotterynumber>
Keep It Simple Stupid! (KISS)
|
|
|
|
|
that's exactly why i give my loop counters meaningful names whenever i have to nest loops - those mistakes can be a pain to find
for finding the min and max what you have now seems ok. in your min function the if statement contains a & , recognize the difference between & (bitwise and) and && (logical and)
|
|
|
|
|
|
The error around line 50 is because your outer loop is going until i = 10,000; whereas the array is only indexed to 1,000
elements. So, when i = 1,000 you'll get the error as you've gone beyond the bounds of the first dimension of the array.
Just my personal opinion, but I always look for places to make small performance and memory savings. Since your values
are 3-digit numbers you are wasting a lot of memory using int as it is a 32-bit value covering numbers that are
way larger than you'll need. You can use ushort (UInt16 ) that will cover the range of numbers you're
working with and use half the memory.
Also, I believe that the maximum value parameter to Random.Next(int) is exclusive for the
upper bound, meaning the maximum value will never be the number you supply; if you want 999 to be a possible value, you
should supply 1,000 as the maximum and then 999 will be the largest value that it will return.
The performance is bad, currently, because you are looping through all elements of the array with each random number you
produce. If you changed your logic to index into the array on the random number instead of using a loop to get to the
element it would greatly improve the performance. In other words, replace this:
currentNum = randomNumbers.Next(999);
for (int j = 0; j < 1000; ++j)
{
if (lotteryNumbers[j, 0] == currentNum)
{
lotteryNumbers[i, 1]++;
}
}
with this:
currentNum = randomNumbers.Next(1000);
lotteryNumbers[currentNum]++; <--- assuming the array's redefined to just one dimension as suggested below
I noticed that the array is 2 dimensional; I didn't really see any need for having 2 dimensions; I'd just create it
as ushort[] lotteryNumbers = new ushort[1000]; . The index position will simulate the values 0 - 999 and
the value of each element would be its number of occurrences. Also, with this change there is no need to initialize any
values in the array as all the elements will default to 0 when it is created.
Last, you'll need a way to sort the results to get the 10 with the least and 10 with the most occurrences. I'd recommand
sorting the results into a single collection and then pulling the first 10 and last 10 elements; I'll leave that as an
exercise to the reader .
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Thanks for the tips. I'm relatively new to C# so your tips are definitely helpful.
|
|
|
|
|
Hope that someone could help me on this...
By this simple line I can define filderID with value selected in Combobox
int folderID = (int)folderComboBox.SelectedValue;
,but how can I define that value when have instead a comoBox a datagridview on my form and i want some selected row to become a folderID ?
plz help
|
|
|
|
|
By handling a similar event, for the selected row changing in your grid.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
how to get a value from a datagridview cell?
led mike
|
|
|
|
|
yess I have a datagridview that conzist of two columns FOlderID, and Folder name, I want to get Folderid for selected row in datagridview and store it in new variable like on a example with combobox, but here is datagrid and i have problem to get that value
|
|
|
|
|
Ain't me babe wrote: but here is datagrid
Ain't me babe wrote: I have a datagridview
First, DataGridView is not the same thing as DataGrid. So make sure you know exactly which you are using.
Second, there exists perfectly fine documentation for both, freely available, on the MSDN site. See this for DataGridView.[^] You should have no difficulties finding your solution by reading that documentation. Good luck.
led mike
|
|
|
|
|
Hi all,
i have arequirement to import excel sheet to postgresql using C#.
i wrote a code:
try
{
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + txtfilename.Text + ";" + "Extended Properties=Excel 8.0;");
mycommand = new OleDbDataAdapter(@"select * from [Ranjit$]", connection);
mydataset = new DataSet();
mycommand.Fill(mydataset, "ExcelInfo");
try
{
dataGridView1.DataSource = mydataset.Tables["ExcelInfo"].DefaultView;
}
catch (Exception ex)
{
//System.Diagnostics.Debug.WriteLine(ex.GetBaseException().ToString());
//MessageBox.Show(ex.GetBaseException().ToString());
throw new Exception(ex.Message);
}
So mydataset is displaying data in datagridview. this is working fine
No i have to send data to postgresql database
For that i wrote code
NpgsqlConnection strconn = new NpgsqlConnection(@"server=localhost;user id=postgres;password=thinksoft10@;database=Test;SyncNotification=true ");
strconn.Open();
NpgsqlCommand cmd = new NpgsqlCommand("COPY \"Test\" FROM STDIN", strconn);
NpgsqlCopyIn cin = new NpgsqlCopyIn(cmd, strconn);
cin.start();
but this is copying files to the database.
So please tell me how to copy that information in the postgresql database.
in my application i am able to show the excel sheet data in datagrid.
but i am not aable to send the excel sheet data to postgresql Test database test table.
Any help is appreciated!!!
thanks in anticipation
Ranjit.balu
|
|
|
|
|
If you already have the data correctly in a datagrid, can't you simply iterate the data in the datagrid and using INSERT INTO statement put the data into the database.
Note: If that's a real username and password in your connection string, I believe you don't want to share them with us.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hello,
Where are the button images (move first, move last etc)
associated with the bindingNavigator buttons ?
I've tried a search, but cannot find them.
thanks
pcjd
|
|
|
|
|
They are set in designer generated file. The images are stored in resx file.
|
|
|
|
|
Thank you for your reply Giorgi,
but let me explain a little further.
I have one project where the bindingnavigator button
images are present, and yes they reside in the resx file.
However, I have a second project which has a bindingnavigator
and the button images are not present. So how can I load them ?
With the project that has the images, the image property of
each button has System.Drawing.Bitmap as the value.
How (or where) can I load these images into my new project ?
(without cutting and pasting from the original resx file)
thanks
pcjd
|
|
|
|
|
Hi,
I have a AxWebBrowser hosted inside a form. How can I notify the form whenever the user changes the selections in the comboBoxes hosted inside the browser?
thanx
|
|
|
|