Web Infotech

Web Infotech

  • NA
  • 20
  • 1.4k

How to retrieve value from dataGridView in a varaible.

Oct 21 2017 6:04 AM
How to retrieve data from datatable in a vairable
I have upload excel file contact number in datagridview1. But under button click gridview value not shown in the variable
 
private void btnFileUpload_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Excel Files|*.xls;*.xlsx"; // file types, that will be allowed to upload
dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
{
String path = dialog.FileName; // get name of file
using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open), new UTF8Encoding())) // do anything you want, e.g. read it
{
// ...
}
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(constr);
System.Data.OleDb.OleDbDataAdapter MyDataAdapter = new System.Data.OleDb.OleDbDataAdapter("Select MobileNo from [Sheet1$]", con);
System.Data.DataTable dt = new System.Data.DataTable();
MyDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
if (dataGridView1.Rows.Count > 0)
LbUploadMsg.Text = (dataGridView1.Rows.Count - 1).ToString() + " Mobile Numbers uploaded successfully.";
else
LbUploadMsg.Text = "Mobile Numbers not found in Excel File or Excel file does not contain MobileNo Heading";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
 
 
private void BtnSendSMS_Click(object sender, EventArgs e)
{
try
{
if (CheckBoxConfirmation.Checked == false)
{
CheckBoxConfirmation.Text = "Please Tick Checkbox First";
}
else
{
string Msg = txtMessage.Text + "\n" + txtRegards.Text;
string MobileNumber;
string dataString;
Success = 0;
Fail = 0;
RC = dataGridView1.Rows.Count;
dataGridView1.DataSource=dt;
CheckBoxConfirmation.Checked = false;
for (int MM = 0; MM < RC; MM++)
{
MobileNumber = dt.Tables[0].Rows[MM][0].ToString();
dataString = obj.SendSMS(Msg, MobileNumber);
if (dataString.Contains("Success") == true)
Success++;
else
Fail++;
CheckBoxConfirmation.Text = Success + " SMS Successfully Sent " + Fail + " SMS Failed";
}
dt = null;
txtMessage.Text = "";
txtNumber.Text = "";
}
}
catch
{
CheckBoxConfirmation.Text = "Problem in Sending SMS !!";
}
 
 
The Mobile Numbers are not uploaded in the MobileNumber from dataGridView1. Please provide me solution.

Answers (9)