Thursday, April 24, 2008

Selecting checkbox in gridview and select values from gridview rows





CheckBox ch = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox1");
if (ch.Checked == true)
{
foreach (GridViewRow gv in GridView1.Rows)
{
ch2 = (CheckBox)gv.FindControl("CheckBox2");

ch2.Checked = true;
string workordernum = gv.Cells[1].Text;
string name = gv.Cells[2].Text;
string dept = gv.Cells[3].Text;
string workorder = gv.Cells[4].Text;
string equipment = gv.Cells[5].Text;
string worktype = gv.Cells[6].Text;
con.Open();
Label6.Text = "insert into jobapprove values('" + Label5.Text.ToString() + "','" + workordernum.ToString() + "','" + name.ToString() + "','" + dept.ToString() + "','" + workorder.ToString() + "','" + equipment.ToString() + "','" + worktype.ToString() + "')";
SqlCommand cmdd = new SqlCommand("insert into jobapprove values('" + Label5.Text.ToString() + "','" + workordernum.ToString() + "','" + name.ToString() + "','" + dept.ToString() + "','" + workorder.ToString() + "','" + equipment.ToString() + "','" + worktype.ToString() + "')", con);
cmdd.ExecuteNonQuery();
con.Close();
}
}
if (ch.Checked == false )
{
foreach (GridViewRow gv in GridView1.Rows)
{
ch2 = (CheckBox)gv.FindControl("CheckBox2");
ch2.Checked = false;
}
}


Coding For selecting Entire Checkbox while selecting Checkbox in Gridview HeaderRow

ch = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");
foreach (GridViewRow gr in GridView1.Rows)
{
if (ch.Checked == true)
{
CheckBox ch1 = (CheckBox)gr.FindControl("CheckBox1");
ch1.Checked = true;
}
else if (ch.Checked == false)
{
CheckBox ch1 = (CheckBox)gr.FindControl("CheckBox1");
ch1.Checked = false;
}

}

Friday, April 18, 2008

Send a Parameter from a form to show in Report Viewer

Things are do before running this from






using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page

{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=vijay;Integrated Security=True");
private ReportDocument rep=new ReportDocument ();
//protected override void unload(object sender, EventArgs e)
//{
// base.OnUnload(e);
// this.Unload += new EventHandler(Page.Unload);
//}
protected void page_unload()
{
rep.Clone();
rep.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{rep.Load (Server.MapPath ("CrystalReport1.rpt"));
rep.FileName = Server.MapPath("CrystalReport1.rpt");
if (!IsPostBack)

{
binddata();
}

}
public void binddata()
{
SqlDataAdapter dr = new SqlDataAdapter("select * from sample", con);
DataSet ds = new DataSet();
dr.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.Items.Insert(0, "Select");
DropDownList1.DataTextField = "username";
DropDownList1.DataValueField = "username";
DropDownList1.DataBind();

DropDownList2.DataSource = ds;
DropDownList2.Items.Insert(0, "Select");
DropDownList2.DataTextField = "num";
DropDownList2.DataValueField = "num";
DropDownList2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = "Connect";
//string name = DropDownList1.SelectedValue.ToString();
//int num = Convert.ToInt32(DropDownList2.SelectedValue);
if (Button1.Text == "Connect")
{
Button1.Text = "View Report";
string name = TextBox1.Text;
int num = Convert.ToInt32(TextBox2.Text.ToString());
rep.SetParameterValue("name", name);
rep.SetParameterValue("num", num);
CrystalReportViewer1.ReportSource = rep;
}
}
}