how can i show the alert message for a button control in windows forms?

Posted by Sudheep.grandhe under C# on 1/12/2012 | Points: 10 | Views : 1875 | Status : [Member] | Replies : 5
Hi,

currently,I am developing windows applications.in my form i am having one gridview,one update and delete button .if the user directly selects the buttons .it has to show the message as "please select value from grid".how can i do this.please do the needful.

public partial class UpdateProduct : Form
{
private string connectionString = null;
private SqlConnection sqlConnection = null;
private SqlDataAdapter sqlDataAdapter = null;
private SqlCommandBuilder sqlCommandBuilder = null;
private DataTable dataTable = null;
private BindingSource bindingSource = null;
private string selectQueryString = null;

public UpdateProduct()
{
InitializeComponent();
}

private void UpdateProduct_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
selectQueryString = "select * from tbl_product";
conn.Open();
sqlDataAdapter = new SqlDataAdapter(selectQueryString, conn);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter );
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataGridView1.DataSource = bindingSource;

}

private void btnupdate_Click(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
MessageBox.Show("Updated Successfully");
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}

private void btndelete_Click(object sender, EventArgs e)
{
try
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
MessageBox.Show("Are you Sure Want To Delete This Row?", "DeleteConformation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
}
}

Best,
Sudheep.



Responses

Posted by: Ujjal on: 1/14/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Sudheep
Try The below mentioned step hopefully it will work 4 u

DialogResult result1 = MessageBox.Show(" Are You Sure You Wants To Delete This Data", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result1 == DialogResult.Yes)
{
WRITE YOUR CODE FOR UPDATE/DELETE ......................
}

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Thanks for replying to me.I need for update button also.can you please send the code for me.please do the needful.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ujjal on: 1/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,

The above code will work for all the buttons you just have to change the message and the code inside if section .



Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

thanks for replying to me.it's not working.

private void btnupdate_Click(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
if (dataGridView1.Rows[rowindex].Selected)
{

MessageBox.Show("Updated Successfully");
}
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
private void btndelete_Click(object sender, EventArgs e)
{

try
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
if (dataGridView1 .Rows [rowindex ].Selected )
MessageBox.Show("Are you Sure Want To Delete This Row?", "DeleteConformation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}

where i have to write the code.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ujjal on: 1/17/2012 [Member] Starter | Points: 25

Up
0
Down
Modify ur code for update and delete button like below code.

private void btnupdate_Click(object sender, EventArgs e)
{
try
{

DialogResult result1 = MessageBox.Show(" Are You Sure You Wants To Delete This Data", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result1 == DialogResult.Yes)
{

sqlDataAdapter.Update(dataTable);

if (dataGridView1.Rows[rowindex].Selected)

{

MessageBox.Show("Updated Successfully");
}
}

}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}


}

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response