我想将以下程序更改为从数据库中获取双精度列表(四舍五入到小数点后两位并按升序排列)并将它们显示在数据网格中的程序。我当前的程序有点无限上下滚动,但它是肤浅的,因为数字会立即被遗忘。我希望我的程序能够在可滚动表中显示数据库中的所有数字,并且我希望数据网格能够记住它们。
唯一的问题是我对数据库没有任何经验。我不知道如何连接到它们,如何导入它们或类似的东西。有人可以帮我开始吗?我至少需要访问数据库(以及哪个),我想我可以自己弄清楚如何格式化它。
另外,对我来说重要的问题:是从在线访问数据库还是可以在本地下载和访问数据库。
谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SterlingDataGrid
{
public partial class Form1 : System.Windows.Forms.Form
{
// private SterlingLib.STIQuote stiQuote = new SterlingLib.STIQuote();
//private SterlingLib.STIApp stiApp = new SterlingLib.STIApp();
//private List<string> listMsg = new List<string>();
// private bool bModeXML = true;
private Panel buttonPanel = new Panel();
private Panel buttonPanel2 = new Panel();
private DataGridView sterlingDataGridView = new DataGridView();
private Button upButton = new Button();
private Button downButton = new Button();
private TextBox quoteBox = new TextBox();
public static double num1 = 1203.5;
public Form1()
{
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
SetupLayout();
SetupDataGridView();
PopulateDataGridView();
}
private void upButton_Click(object sender, EventArgs e)
{
num1 += .1;
sterlingDataGridView.Rows[1].Cells[2].Value = num1;
for (int r = 2; r < 10; r++)
{
sterlingDataGridView.Rows[r].Cells[2].Value = (double)(sterlingDataGridView.Rows[r - 1].Cells[2].Value) - 0.1;
}
}
private void downButton_Click(object sender, EventArgs e)
{
num1 -= .1;
sterlingDataGridView.Rows[1].Cells[2].Value = num1;
for (int r = 2; r < 10; r++)
{
sterlingDataGridView.Rows[r].Cells[2].Value = (double)(sterlingDataGridView.Rows[r - 1].Cells[2].Value) - 0.1;
}
}
//***NEW***
private void sterlingDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo hit = sterlingDataGridView.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell)
{
//clickedCell = sterlingDataGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
if(hit.ColumnX == 0)
MessageBox.Show("TEST COMPLETE. ROW INDEX: " + hit.RowIndex + " COLUMN INDEX: " + hit.ColumnIndex);
}
}
}
private void SetupLayout()
{
this.Size = new Size(600, 500);
sterlingDataGridView.AutoSize = true;
upButton.Text = "^";
upButton.Location = new Point(10, 10);
upButton.Click += new EventHandler(upButton_Click);
downButton.Text = "v";
downButton.Location = new Point(100, 10);
downButton.Click += new EventHandler(downButton_Click);
sterlingDataGridView.MouseDown += new MouseEventHandler(sterlingDataGridView_MouseDown);
buttonPanel.Controls.Add(upButton);
buttonPanel.Controls.Add(downButton);
buttonPanel.Controls.Add(quoteBox);
quoteBox.Location = new Point(100, 0 + (upButton.Height*2));
downButton.Location = new Point(110, 0 + upButton.Height);
upButton.Location = new Point(110, 0);
buttonPanel.Height = 197;
buttonPanel.Dock = DockStyle.Bottom;
buttonPanel2.Width = 231;
buttonPanel2.Dock = DockStyle.Right;
// buttonPanel.Controls.
this.Controls.Add(this.buttonPanel);
this.Controls.Add(this.buttonPanel2);
}
private void SetupDataGridView()
{
this.Controls.Add(sterlingDataGridView);
sterlingDataGridView.ColumnCount = 6;
sterlingDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
sterlingDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
sterlingDataGridView.ColumnHeadersDefaultCellStyle.Font =
new Font(sterlingDataGridView.Font, FontStyle.Bold);
sterlingDataGridView.Name = "sterlingDataGridView";
sterlingDataGridView.Location = new Point(8, 8);
sterlingDataGridView.Size = new Size(500, 250);
sterlingDataGridView.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
sterlingDataGridView.ColumnHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Single;
sterlingDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
sterlingDataGridView.GridColor = Color.Black;
sterlingDataGridView.RowHeadersVisible = false;
sterlingDataGridView.ReadOnly = true; //***NEW***
sterlingDataGridView.Columns[0].Width = sterlingDataGridView.Columns[2].Width / 2;
sterlingDataGridView.Columns[1].Width = sterlingDataGridView.Columns[2].Width/2;
sterlingDataGridView.Columns[3].Width = sterlingDataGridView.Columns[2].Width / 2;
sterlingDataGridView.Columns[4].Width = sterlingDataGridView.Columns[2].Width / 2;
sterlingDataGridView.Columns[5].Width = sterlingDataGridView.Columns[2].Width / 2;
sterlingDataGridView.Columns[0].Name = "Bid";
sterlingDataGridView.Columns[1].Name = "BidQty";
sterlingDataGridView.Columns[2].Name = "";
sterlingDataGridView.Columns[3].Name = "AskQty";
sterlingDataGridView.Columns[4].Name = "Ask";
sterlingDataGridView.Columns[5].Name = "Vol";
sterlingDataGridView.SelectionMode =
DataGridViewSelectionMode.FullRowSelect;
sterlingDataGridView.MultiSelect = false;
sterlingDataGridView.Dock = DockStyle.Fill;
}
void PopulateDataGridView()
{
double num2 = num1 - .1;
double num3 = num2 - .1;
double num4 = num3 - .1;
double num5 = num4 - .1;
double num6 = num5 - .1;
double num7 = num6 - .1;
double num8 = num7 - .1;
double num9 = num8 - .1;
string[] row0 = { "", "", " ^", "", "", "" };
string[] row1 = { "", "", System.Convert.ToString(num1), "", "", "" };
string[] row2 = { "", "", System.Convert.ToString(num2), "", "", "" };
string[] row3 = { "", "", System.Convert.ToString(num3), "", "", "" };
string[] row4 = { "", "", System.Convert.ToString(num4), "", "", "" };
string[] row5 = { "", "", System.Convert.ToString(num5), "", "", "" };
string[] row6 = { "", "", System.Convert.ToString(num6), "", "", "" };
string[] row7 = { "", "", System.Convert.ToString(num7), "", "", "" };
string[] row8 = { "", "", System.Convert.ToString(num8), "", "", "" };
string[] row9 = { "", "", System.Convert.ToString(num9), "", "", "" };
string[] row10 = { "", "", " v", "", "", "" };
sterlingDataGridView.Rows.Add(row0);
sterlingDataGridView.Rows.Add(row1);
sterlingDataGridView.Rows.Add(row2);
sterlingDataGridView.Rows.Add(row3);
sterlingDataGridView.Rows.Add(row4);
sterlingDataGridView.Rows.Add(row5);
sterlingDataGridView.Rows.Add(row6);
sterlingDataGridView.Rows.Add(row7);
sterlingDataGridView.Rows.Add(row8);
sterlingDataGridView.Rows.Add(row9);
sterlingDataGridView.Rows.Add(row10);
sterlingDataGridView.Columns[0].DisplayIndex = 0;
sterlingDataGridView.Columns[1].DisplayIndex = 1;
sterlingDataGridView.Columns[2].DisplayIndex = 2;
sterlingDataGridView.Columns[3].DisplayIndex = 3;
sterlingDataGridView.Columns[4].DisplayIndex = 4;
}
}
}