Get more information on GridView row click - c#

I need to get this : When I click on a row, to get a more information about that particular file(every row represents one file from database), and that information is stored in different table... So, how is the best way to do this? Here you have a picture...
public partial class WebForm1 : System.Web.UI.Page
{
Web_service.WebService1SoapClient service;
protected void Page_Load(object sender, EventArgs e)
{
service = new Web_service.WebService1SoapClient();
BindData();
}
protected void BindData()
{
string strConnection = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from request", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
r.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
r.Attributes["onmouseout"] = "this.style.textDecoration='none';";
r.ToolTip = "Click to select row";
r.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + r.RowIndex, true);
}
}
base.Render(writer);
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}
}

Related

choose name from dropownlist and associated ID appear in textbox

this is my code for populating a dropdown list with customers name.
protected void Page_Load(object sender, EventArgs e)
{
//Creating a connection to my database using the connection string
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
{
SqlCommand cmd = new SqlCommand("Select CustomerName from Customer", con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataTextField = "CustomerName";
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
}
I want to be able too; when this name is chosen from the dropdownlist box , the associated customer ID will appear in a textbox.
thanks!
It should be as simple as this, by the looks of it:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue
}
Assuming you have a textbox whose ID is TextBox1.
Alternatively if you want to display the text from the dropdown list (as opposed to the ID value), then simply:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedItem.Text
}

C# Master Detail gridview on SelectionChanged

I have one form with grid(dataGridView1) and textbox(txtSearch). When I type something in textbox grid filter by field acSubject. Now I put second grid and I want new Custom SQL query which will be depend on selected row in dataGridView1.
SQL would be:
select anUserID from the_setsubjcontact where acSubject = #acSubject
How can I do this?
Code is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=local\s08r2;Initial Catalog=Demo;User id=sa;Password=sa";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(#"
SELECT acSubject, acAddress, acPost, acName, acPhone,
acFieldSA, acFieldSB, acFieldSC, acFieldSD, acFieldSE,
anFieldNA, anFieldNB, anFieldNC, anFieldND, anFieldNE, OdgovornaOsoba, acSubjTypeBuyer
FROM ARS.dbo._ARSCRM_vSubjekti
", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;
}
else
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("acSubject like '%{0}%'", txtSearch.Text);
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//User selected a cell (show the first cell in the row)
if (dgv.SelectedCells.Count > 0)
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[5].Value.ToString();
}
}
I was success when I use DataSet from C#, but with CustomSQL I don't know how to do that.
private void ShowDetails(int UserId)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=local\s08r2;Initial Catalog=Demo;User id=sa;Password=sa";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(#"
select anUserID from the_setsubjcontact where acSubject = #acSubjec", con);
da.SelectCommand.Parameters.AddWithValue(#acSubjec, UserId.ToString());
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView2.DataSource = dt;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//User selected a cell (show the first cell in the row)
if (dgv.SelectedCells.Count > 0 && dgv.SelectedCells[0].RowIndex >-1 && dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells.Count > 0)
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
ShowDetails(int.Parse(txtAcFieldSA.Text));
}

search, edit and delete using gridview with sql server in c#

i am using Gridview buttons to edit and delet records from sql DB , the problem that i use specific criteris (search by textbox and combobox) then i do edit for the results here is the code i use :
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["zamzammembersConnectionString"].ConnectionString);
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");
TextBox textname = (TextBox)row.FindControl("textbox1");
TextBox textmarks = (TextBox)row.FindControl("textbox2");
GridView1.EditIndex = -1;
conn.Open();
SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
public void bind()
{
conn.Open();
SqlCommand com = new SqlCommand("select zam_pcinfo.employe_name as Name , zam_location.locationname as Location from zam_pcinfo inner join zam_location on zam_pcinfo.location_id = zam_location.locationId where zam_pcinfo.employe_name like #name or zam_location.locationname like #locationname;", conn);
com.Parameters.AddWithValue("#name", SqlDbType.VarChar).Value = nametxt.Text;
com.Parameters.AddWithValue("#locationname", SqlDbType.VarChar).Value = locationdrop.SelectedValue;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)row.FindControl("lblid");
conn.Open();
SqlCommand cmd = new SqlCommand("delete emp where rowid=" + lbldeleteID.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
}
when i try to make the same code in bind() method to a button it works fine but i couldn't do edit and delete on it . how can i make edit and delete in gridview with data that i search for ?

GridView does not change on page change

Gridview: Data does not change on page change.
Hi, im working on a asp.net website with c#.
I have a gridview with paging, the gridview is populated from an sql database, and on the PageIndexChanging I have:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGridControl();
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}
If I click on page 2 for example, the next rows of data display fine.
But if I click back to the page 1, the data does not change, it keep displaying the data from the page 2.
How can I solve this?
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
BindGridControl();
gridView.DataBind();
}
Is your BindGridControl() load the data and set it to your gridView datasource?
You can reverse order of instructions
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
BindGridControl();
gridView.DataBind();
}
I had this problem too for a long time.
I came up with this and it finally works for me.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGridControl(e.NewPageIndex);
}
And then in your BindGridControl function:
protected void BindGridControl(int pageNumber = 0)
{
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[connect‌​ion].ConnectionStrin‌​g);
SqlDataReader dr;
SqlCommand cmd;
cmd = new SqlCommand();
cmd.Connection = sqlConn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_load";
sqlConn.Open();
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
GridView1.PageIndex = pageNumber;
GridView1.DataSource = dt;
GridView1.DataBind();
sqlConn.Close();
}
Try
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
BindGridControl();
DataBind();
}

crystal reports using parameters

i am using crystal reports for the first time. I have written code which is as follows
public partial class _Default : System.Web.UI.Page
{
private ReportDocument report = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
report.Load(Server.MapPath("CrystalReport1.rpt"));
report.FileName = Server.MapPath("CrystalReport1.rpt");
if (!Page.IsPostBack)
{
BindData();
}
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
this.Unload+=new EventHandler(Page_Unload);
}
public void Page_Unload(object sender, EventArgs e)
{
report.Clone();
report.Dispose();
}
private void BindData()
{
Trusted_Connection=true";
string connectionString = #"Data Source=WINSERVER;Initial Catalog=card;User ID=sa;Password = db2admin";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT name,address,idno FROM iffcar", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "idno";
DropDownList1.DataBind();
}
protected void Btn_DisplayReport(object sender, EventArgs e)
{
int idno = Convert.ToInt32(DropDownList1.SelectedValue);
report.SetParameterValue("idno", idno);
CrystalReportViewer1.ReportSource = report;
}
}
i am getting values in the dropdownlist but,
Now my problem is once i select a value from dropdownlist i want report of that value.
how to do it.....
please help me to solve my problem.
Try using the index number of the parameter:
protected void Btn_DisplayReport(object sender, EventArgs e)
{
int idno = Convert.ToInt32(DropDownList1.SelectedValue);
report.SetParameterValue(0, idno);
CrystalReportViewer1.ReportSource = report;
}

Categories