Getting data from a selected row in a gridview
sno name fname add dob mobile email
1 pavan raju hyd 1/02/1990 9898899999 svx@fg
2 naveen praveen hyd 2/1/1989 987345666 jkl@iu
i wnt output like this
when i select row in gridview select coloums name fname add dob ....selct four coloums in row( name fname add dob) send sms to mobile plz write the quarie
pavan raju hyd 1/02/1990 to mobile(9898899999)
Hemant SrivastavaPosted Feb 1, 2013, 3:25 PM
I tried with Web Application instead of Windows App and used GridView. In the loop, where you are retrieving data in the lists, that part is fine. Its working fine too in my web app.
The problem is in only in the line:
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
JAYRAMPosted Feb 1, 2013, 4:08 AM
please make correction
protected void btnSend_Click(object sender, EventArgs e)
{
List
List
if (gv.Rows.Count > 0)
{
for (int i = 0; i <= gv.Rows.Count - 1; i++)
{
GridViewRow row = gv.Rows[i];
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked == true)
{
mobilenumbers.Add((gv.Rows[i].Cells[5].Text));
data.Add((gv.Rows[i].Cells[2].Text) + (gv.Rows[i].Cells[4].Text)+(gv.Rows[i].Cells[6].Text));
//data.Add(row.Cells["NAME"].value.Tostring() + "\t");
}
}
clssms obj = new clssms();
for (int j = 0; j <= mobilenumbers.Count - 1; j++)
{
for (int k=0; k<=data.Count-1; k++)
{
string str = obj.SendSMS("****", "****", "91" + mobilenumbers[j] + "", data[j], "SMSCntry", "N", "N");
Response.Write(str);
Hemant SrivastavaPosted Jan 30, 2013, 6:05 PM
public class PersonalRecord
{
public bool IsChecked { get; set; }
public int sno { get; set; }
public string name { get; set; }
public string fname { get; set; }
public string add { get; set; }
public string dob { get; set; }
public string mobile { get; set; }
public string email { get; set; }
public PersonalRecord(int sn, string nm, string fn, string ad, string db, string mb, string eml)
{
sno = sn;
name = nm;
fname = fn;
add = ad;
dob = db;
mobile = mb;
email = eml;
}
}
and bind the grid in the following way:
private void Form1_Load(object sender, EventArgs e)
{
List
{
new PersonalRecord(1,"pavan","raju","hyd","1/02/1990","9898899999","svx@fg"),
new PersonalRecord(2,"naveen","praveen","hyd","2/1/1989","987345666","jkl@iu"),
new PersonalRecord(3,"mohan","shri","hyd","2/1/1979","98732266","mnk@iu"),
new PersonalRecord(4,"kapil","dev","Del","2/1/1969","981125666","kpll@iu"),
};
dataGridView1.DataSource = lstSession;
Hope it would help. If it solves ur problem, mark this thread as accepted.dataGridView1.ClearSelection();
}
Thanks,
Hemant
Hemant SrivastavaPosted Jan 30, 2013, 5:57 PM
and get output like this:
Hemant SrivastavaPosted Jan 30, 2013, 5:54 PM
private void btn_ShowSelecteddata_Click(object sender, EventArgs e) mobilenumbers = new List(); data = new List();
{
List
List
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells["IsChecked"].Value) == true)
{
mobilenumbers.Add(row.Cells["mobile"].Value.ToString());
data.Add(row.Cells["name"].Value.ToString() + "\t" +
row.Cells["fname"].Value.ToString() + "\t" +
row.Cells["add"].Value.ToString() + "\t" +
row.Cells["dob"].Value.ToString() + "\t");
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mobilenumbers.Count; i++)
{
sb.Append("Send SMS to " + mobilenumbers[i] + " with message \t" + data[i] +"\n\n");
}
MessageBox.Show(sb.ToString());
}
JAYRAMPosted Jan 29, 2013, 10:37 PM
List
List
if (gv.Rows.Count > 0)
{
for (int i = 0; i <= gv.Rows.Count - 1; i++)
{
GridViewRow row = gv.Rows[i];
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked == true)
{
data.Add((gv.Rows[i].Cells[1].ToString()+gv.Rows[i].Cells[2].ToString()+gv.Rows[i].Cells[3].ToString()+gv.Rows[i].Cells[4].ToString());
mobilenumbers.Add ((gv.Rows[i].Cells[5].Text));
}
}
clssms obj = new clssms();
for (int j = 0; j <= mobilenumbers.Count - 1; j++)
{
string str = obj.SendSMS("name", "password", "91"+mobilenumbers[j]+"", data.Text, "SMSCntry", "N", "N");
Hemant SrivastavaPosted Jan 29, 2013, 7:52 PM
then try this code,
DataGridViewRow row = dataGrid1.SelectedRows[0];
string data = row["name"].Value.ToString()
+ row["fname"].Value.ToString()
+ row["add"].Value.ToString()
+ row["dob"].Value.ToString();
string mobileNum = row["name"].Value..ToString();