ok so I made a datagridview. when I enter the primary key it will
display the data. what I want to do next is make it so that when I
enter another primary key it will display the next data and the
previous data. heres what I got
public void loadDataGrid(string sqlQueryString)
{
OleDbCommand SQLQuery = new OleDbCommand();
DataTable data = null;
dataGridView1.DataSource = null;
SQLQuery.Connection = null;
OleDbDataAdapter dataAdapter = null;
dataGridView1.Columns.Clear();
SQLQuery.CommandText = sqlQueryString;
SQLQuery.Connection = database;
data = new DataTable();
dataAdapter = new OleDbDataAdapter(SQLQuery);
dataAdapter.Fill(data);
dataGridView1.DataSource = data;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ReadOnly = true;
dataGridView1.Columns[0].Visible = false;
}
currently it just displays the recent data only, it doesnt show the previous ones.. is there a command to do this?
Loading
kiran kumarPosted Feb 22, 2010, 11:00 PM
PLEASE MARK THE ANSWER IF IT HELPS YOU
wsdfgPosted Mar 2, 2010, 3:53 PM
now I want to add some more functionality. this time I want to make so that if I input a cardnum thats already being displayed. it removes the row from the datagridview (not the database, just in the datagridview) heres what I got so far
private void CheckKeys3(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
string card = textBox1.Text.ToString();
int cardnum = int.Parse(card);
park = park - 1;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=car.mdb");
con.Open();
OleDbCommand queryString = new OleDbCommand("SELECT cardnum, ownerf, ownerl, make, model, year, color, chassisnumber, platenumber, controlnumber FROM vehicle WHERE vehicle.cardnum = " + cardnum + "",con);
OleDbDataReader dr = queryString.ExecuteReader();
if (dr.Read())
{
DataGridViewRow row = new DataGridViewRow();
this.dataGridView1.Rows.Add(row);
dataGridView1.Rows[i].Cells[0].Value = dr["cardnum"].ToString();
dataGridView1.Rows[i].Cells[1].Value = dr["ownerf"].ToString();
dataGridView1.Rows[i].Cells[2].Value = dr["ownerl"].ToString();
dataGridView1.Rows[i].Cells[3].Value = dr["make"].ToString();
dataGridView1.Rows[i].Cells[4].Value = dr["model"].ToString();
dataGridView1.Rows[i].Cells[5].Value = dr["year"].ToString();
dataGridView1.Rows[i].Cells[6].Value = dr["color"].ToString();
dataGridView1.Rows[i].Cells[7].Value = dr["chassisnumber"].ToString();
dataGridView1.Rows[i].Cells[8].Value = dr["platenumber"].ToString();
dataGridView1.Rows[i].Cells[9].Value = dr["controlnumber"].ToString();
int k=i;
do
{
k = k - 1;
if (k >=0)
{
string wah = dataGridView1[0, k].Value.ToString();
int weh = int.Parse(wah);
if (cardnum == weh)
{
dataGridView1.Rows.RemoveAt(k);
dataGridView1.Rows.RemoveAt(i);
}
}
} while (k >=0);
i++;
}
con.Close();
}
the bold text is my attempt. but its messed up. any help pls
wsdfgPosted Mar 1, 2010, 1:19 PM
"No row can be added to a DataGridView control that does not have columns. Columns must be added first."
wsdfgPosted Feb 24, 2010, 12:33 PM
kiran kumarPosted Feb 24, 2010, 12:23 PM
wsdfgPosted Feb 24, 2010, 12:07 PM
ExecuteReader: Connection property has not been initialized.
kiran kumarPosted Feb 23, 2010, 11:12 AM
Oledbconnection con = new Oledbconnection(your connection string);
replace all class related to sqlclient to oledb
if it is sqlcommand replace it with oledbcommand
if it is sqldatareader->oledbdatareader
sqldataadapter->oledbdataadapter
PLEASE MARK THE ANSWER IF IT HELPS YOU
wsdfgPosted Feb 23, 2010, 7:00 AM
SqlConnection con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=kiran;Integrated Security=True");
where do I instance my database. I've only used OleDb before so I dont know how to in your code.
wsdfgPosted Feb 22, 2010, 8:53 PM
private void CheckKeys3(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
string card = textBox1.Text.ToString();
int cardnum = int.Parse(card);
park = park - 1;
string queryString = "SELECT cardnum, ownerf, ownerl, make, model, year, color, chassisnumber, platenumber, controlnumber FROM vehicle WHERE vehicle.cardnum = " + cardnum + "";
loadDataGrid(queryString);
if (park > 0)
{
textBox2.Text = "available";
}
if (park < 1)
{
textBox2.Text = "full";
}
textBox1.Clear();
}
}
thats for the query string, note since I am querying using the primary key. only 1 row of data will be displayed on the datagridview. what I want is to make it so that if I make another query the previous row of data will still be there along with the newly queried row of data. anyway to do this?
Sam HobbsPosted Feb 22, 2010, 7:49 PM