Get Seleted Data From Gridview,
In This article i will give a code snippet to get seleted data from gridview on clicking a particular Row's Button ,you can get data from it.
There is no checkbox used in it,instead of checkbox i used button here.and you can get data dynamically from gridview,
First of all bind your gridview ,You can use any method for binding gridview .here i am use linq to sql.
Now here is the code for THAT,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLayer;
namespace EventManagerApp.SuperAdmin
{
public partial class Article : System.Web.UI.Page
{
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
grdBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void grdBind()
{
try
{
EventManagerDataContext db = new EventManagerDataContext();
var q = from a in db.EMR_INVOICEs
join b in db.EMR_ACCOUNTs on a.Account_ID equals b.Account_ID
select new
{
CompanyName = b.CompanyName,
Number = a.Invoice_number,
Invoice_date = a.Invoice_date,
Invoice_total = a.Invoice_total,
Invoice_ID=a.Invoice_ID
};
grid.DataSource = q.ToList();
grid.DataBind();
}
catch
{
throw;
}
}
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
EventManagerDataContext db = new EventManagerDataContext();
var q = from a in db.EMR_INVOICEs
join b in db.EMR_ACCOUNTs on a.Account_ID equals b.Account_ID
select new
{
CompanyName = b.CompanyName,
Number = a.Invoice_number,
Invoice_date = a.Invoice_date,
Invoice_total = a.Invoice_total,
Invoice_ID = a.Invoice_ID
};
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void lnkPdf_Click(object sender, EventArgs e)
{
Button lnkPDf = (Button)sender;
GridViewRow clickedRow = (GridViewRow)lnkPDf.NamingContainer;
Label Invoice_date = (Label)clickedRow.FindControl("lbldate");
string dateofinvoice = Invoice_date.Text;
LinkButton CompanyName = (LinkButton)clickedRow.FindControl("lblaccount");
string nameofcompany = CompanyName.Text;
Label Number = (Label)clickedRow.FindControl("lblnumber");
string invoicenumber = Number.Text;
Label Invoice_total = (Label)clickedRow.FindControl("lblamount");
string totalofinvoice = Invoice_total.Text;
int index = clickedRow.RowIndex;
lblMsg.Text = "Selected Date of Invoice: " + dateofinvoice + "
" + "Selected CompanyName: " + nameofcompany + "" + "Selected InvoiceNumber: " + invoicenumber + "
" + "Selected total of invoice: " + totalofinvoice + "
" + "Selected subject of invoice: " ;
}
}
}
with this code you can get selected data from gridview.I hope This code snippet used for develop many functionality in c# Dotnet
I also Give snapshots for this it.
Thanks&Regards
Ketan Italiya
First of all thank for posting using article . GridView is only used to display data, he cannot retrieve data as you expect, actually you should use the ListView.ItemContainerGenerator.ItemFromContainer call. But I still don't understand your question, when you use data binding to display data, the data source is at your control, you can do anything against it in the code behind, why do you have to retrieve the data by manipulating the view. . You can also apply this code snippet for it