listview.
-
hi.. why when i drag out a listview in vs 2005. i cant get teh function of listview.selectedindex ? only selecteditem .. i want to get the id of the data selected ..
You have SelectedItems and SelectedIndices - check the Count property of either one of these to make sure there is at least one item selected and if you're just wanting the first item selected then something like this:
int firstSelectedIndex1 = listView1.SelectedItems[0].Index;
int firstSelectedIndex2 = listView1.SelectedIndices[0];Dave
-
You have SelectedItems and SelectedIndices - check the Count property of either one of these to make sure there is at least one item selected and if you're just wanting the first item selected then something like this:
int firstSelectedIndex1 = listView1.SelectedItems[0].Index;
int firstSelectedIndex2 = listView1.SelectedIndices[0];Dave
hi.. sorry.. i think i would like to use up the DataGridView called dvCustomer under private void dvCustomer_SelectionChanged(object sender, EventArgs e) { } when i click on the datagridview row... i would like to get the user ID from database. so that i can do the update as below ..
private void cust_update_Click(object sender, EventArgs e)
{
bool success;
success = dataAccess.AddCustomer(Datagridview ID, cust_name.Text, cust_add.Text, cust_account.Text, cust_phone.Text...);
if (success)
{
MessageBox.Show("Data updated!");
}
else
{
MessageBox.Show("Update FAIL!!!");
}
}in my dataAccess class
public bool AddCustomer(string function, string custName, string custAdd, string custAcc, string custPhone.....)
{
bool success = false;
string query = string.Empty;
try
{
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(new SqlParameter("@companyName", custName));
cmd.Parameters.Add(new SqlParameter("@customerAdd", custAdd));
cmd.Parameters.Add(new SqlParameter("@attName", custAdd));
cmd.Parameters.Add(new SqlParameter("@customerAcc", custAcc));
cmd.Parameters.Add(new SqlParameter("@customerPhone", custPhone));query += "UPDATE CUSTOMER set company_name= @companyName, company_add= @customerAdd, att_name= @attName, account_num= @customerAcc, phone= @customerPhone...........";
query += " where cust_id = ?????????? ";cmd.CommandText = query; if (cn.State == ConnectionState.Closed) cn.Open(); cmd.Connection = cn; if (cmd.ExecuteNonQuery() > 0) { success = true; return success; }
-
hi.. sorry.. i think i would like to use up the DataGridView called dvCustomer under private void dvCustomer_SelectionChanged(object sender, EventArgs e) { } when i click on the datagridview row... i would like to get the user ID from database. so that i can do the update as below ..
private void cust_update_Click(object sender, EventArgs e)
{
bool success;
success = dataAccess.AddCustomer(Datagridview ID, cust_name.Text, cust_add.Text, cust_account.Text, cust_phone.Text...);
if (success)
{
MessageBox.Show("Data updated!");
}
else
{
MessageBox.Show("Update FAIL!!!");
}
}in my dataAccess class
public bool AddCustomer(string function, string custName, string custAdd, string custAcc, string custPhone.....)
{
bool success = false;
string query = string.Empty;
try
{
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(new SqlParameter("@companyName", custName));
cmd.Parameters.Add(new SqlParameter("@customerAdd", custAdd));
cmd.Parameters.Add(new SqlParameter("@attName", custAdd));
cmd.Parameters.Add(new SqlParameter("@customerAcc", custAcc));
cmd.Parameters.Add(new SqlParameter("@customerPhone", custPhone));query += "UPDATE CUSTOMER set company_name= @companyName, company_add= @customerAdd, att_name= @attName, account_num= @customerAcc, phone= @customerPhone...........";
query += " where cust_id = ?????????? ";cmd.CommandText = query; if (cn.State == ConnectionState.Closed) cn.Open(); cmd.Connection = cn; if (cmd.ExecuteNonQuery() > 0) { success = true; return success; }