Setting a button on Gridview row, conditionally - c#

I have a button on my Gridview:
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" />
I am loading my Gridview from SQL, to a Class, then a DataBind() event,
protected void FilterResults(object sender, EventArgs e)
{
var shipments = new List<SoftShipments>();
DateTime dt1 = Convert.ToDateTime(Textbox1.Text);
DateTime dt2 = Convert.ToDateTime(Textbox2.Text);
string cvt1 = "'" + dt1.Year.ToString() + "-" + dt1.Month.ToString() + "-" + dt1.Day.ToString() + "'";
string cvt2 = "'" + dt2.Year.ToString() + "-" + dt2.Month.ToString() + "-" + dt2.Day.ToString() + "'";
string qry = null;
if (Showshipped.Checked)
{
qry = "select * from sft_Ship where sft_Entry_Dt between " + cvt1 + " and " + cvt2;
}
else {
qry = "select * from sft_Ship where sft_Entry_Dt between " + cvt1 + " and " + cvt2 + " and sft_shipped = 'No'";
}
SqlConnection conn = new SqlConnection(Sitecore.Configuration.Settings.GetConnectionString("softship"));
conn.Open();
SqlCommand cmd = new SqlCommand(qry, conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
shipments.Add(new SoftShipments() { index = (int) dr["id"], softtitle = dr["sft_SoftTitle"].ToString(),
productID = dr["sft_ProductID"].ToString(), ver = dr["sft_Version"].ToString(),
custnam = dr["sft_CustName"].ToString(), title = dr["sft_Title"].ToString(),
comp = dr["sft_Company"].ToString(), shipAddr = dr["sft_ShipAddress"].ToString(),
dept = dr["sft_Dept"].ToString(), city = dr["sft_City"].ToString(), state = dr["sft_State"]
.ToString(), postalCd = dr["sft_PostalCd"].ToString(), country = dr["sft_Country"].ToString(),
email = dr["sft_Email"].ToString(), entry_date = dr["sft_Entry_Dt"].ToString(),
ship_date = dr["sft_Ship_Dt"].ToString(), shipped = dr["sft_Shipped"].ToString()
});
}
gdv_Ship.DataSource = shipments;
gdv_Ship.DataBind();
conn.Close();
}
I would like to load the Gridview with the button visible if the value "shipped = 'No' or not visible if 'Yes' ... just not quite certain where to add this code? Any assistance would be appreciated.
Regards,

You could subscribe to the databound event of the grid, and then show/hide the buttons in the template with a FindControl("controlName")

Related

"There is no row at position 0 how to fix it"

During find data between two days, getting error "there is no row at position 0"
MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["Demo"].ConnectionString.ToString());
string str = "select * from sample where name='" + Session["name"] + "' and date between '" + txtfirstdate.Text + "' and '" + txtenddate.Text + "'";
MySqlCommand cmd = new MySqlCommand(str, connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet set = new DataSet();
connection.Open();
adapter.Fill(set);
connection.Close();
//var table = set.Tables[0];
if (set != null)
{
lblname.Text = set.Tables[0].Rows[0]["name"].ToString();
lbldate.Text = set.Tables[0].Rows[0]["date"].ToString();
}
Read the Error Message. Your result set table has no rows.
Replace u r if Condition.
if (set.Tables[0].Rows.Count > 0) {
lblname.Text = set.Tables[0].Rows[0]["name"].ToString();
lbldate.Text = set.Tables[0].Rows[0]["date"].ToString();
}

how to sort listbox using comboboxC#

I want to sort my listbox using combobox,
the combobox will include A-Z and Z-A , so how can I do it and let it work ?
some of my code for the listbox ,lst_OrderName is the one i want to sort.
private void AllorderBySearch()
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("select* from Tbl_order WHERE CustomerNo = '" + txt_CustomerNo.Text + "' And OrderName LIKE '%" + txt_OrderNo.Text + "%' AND OrderName LIKE '%" + txt_OrderNo.Text + "%' AND OrderName LIKE '%" + txt_OrderNo.Text + "%' AND Date between '" + dateTimePicker1.Text + " 00:00:00.000' AND '" + dateTimePicker1.Text + " 23:59:59.999'; ", connection))
{
DataTable Tbl_order = new DataTable();
connection.Open(); //opens the connection
adapter.Fill(Tbl_order);
connection.Close(); //Closes the connection
lst_CustomerNo.DataSource = Tbl_order; //assigns a datasource
lst_CustomerNo.DisplayMember = "CustomerNo"; //assigns display
lst_CustomerNo.ValueMember = "CustomerNo";
lst_OrderName.DataSource = Tbl_order;
lst_OrderName.DisplayMember = "OrderName";
lst_OrderName.ValueMember = "OrderName";
lst_Quantity.DataSource = Tbl_order;
lst_Quantity.DisplayMember = "Quantity";
lst_Quantity.ValueMember = "Quantity";
lst_Price.DataSource = Tbl_order;
lst_Price.DisplayMember = "Price";
lst_Price.ValueMember = "Price";
lst_datetime.DataSource = Tbl_order;
lst_datetime.DisplayMember = "Date";
lst_datetime.ValueMember = "Date";
}
}
I created combobox but i didn't do anything in it yet cuse I dunno how to make it by the way i want . could you help me plz?
you must use a temp object for sorting like bubble sort
As you are using a DataTable as the data source, the following should work:
private void cmbSort_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = lst_CustomerNo.DataSource as DataTable;
if(cmbSort.SelectedItem == "A-Z")
dt.DefaultView.Sort = "OrderName ASC";
else
dt.DefaultView.Sort = "OrderName DESC";
}
Attach the above event to your combobox SelectedIndexChanged action..

Fill an array with sql query

Is there anyway to fill an multidimensional array with a sql query? My sql query results on a pivot table. As you can see, I want so make some sums (for Total Column and Total Row) and I want to know the best way to make that sums and percentages in that table.. I said multidimensional array because it could be easier. The code below works but I want to know how can I choose the rows and columns to make the sums. I'm using ASP.NET C#
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=****;Initial Catalog=***;User=sa;password=***");
//query for all specialities
string specstring = "SELECT Speciality.Shortname, SUM(1) as contar " +
"FROM DoctorEnterpriseDetails INNER JOIN " +
"Speciality ON DoctorEnterpriseDetails.Speciality1 = Speciality.SpecialityId INNER JOIN " +
" GroupType ON DoctorEnterpriseDetails.GroupId = GroupType.GroupId " +
" WHERE (DoctorEnterpriseDetails.EnterpriseId = 48) " +
" GROUP BY Speciality.Shortname ";
SqlCommand command = new SqlCommand(specstring, conn);
command.Connection.Open();
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = command;
// Adds rows in the DataSet
//DataSet myDataSet = new DataSet();
DataTable specstringtable = new DataTable();
myDataAdapter.Fill(specstringtable);
specstring = "";
for (int i = 0; i < specstringtable.Rows.Count; i++)
{
if (specstring == "")
{
specstring = "[" + specstringtable.Rows[i][0] + "]".ToString();
}
else
{
specstring = specstring + ", " + "[" + specstringtable.Rows[i][0] + "]";
}
}
//GroupGrid.DataSource = cobtable;
//GroupGrid.DataBind();
command.Connection.Close();
////query for pivot table
string querystring = "SELECT Description AS Categoria, " + specstring +
"FROM (SELECT GroupType.Description, Speciality.Shortname, SUM(1) AS contar, GroupType.GroupId " +
"FROM DoctorEnterpriseDetails INNER JOIN " +
"Speciality ON DoctorEnterpriseDetails.Speciality1 = Speciality.SpecialityId INNER JOIN " +
"GroupType ON DoctorEnterpriseDetails.GroupId = GroupType.GroupId " +
"WHERE (DoctorEnterpriseDetails.EnterpriseId = 48) " +
"GROUP BY GroupType.Description, Speciality.Shortname, DoctorEnterpriseDetails.GroupId, GroupType.GroupId) as ps " +
"PIVOT (SUM(contar) FOR Shortname IN (" + specstring + ")) pvt " +
"ORDER BY GroupId; ";
////Response.Write(querystring);
SqlCommand command2 = new SqlCommand(querystring, conn);
command2.Connection.Open();
SqlDataAdapter myDataAdapter2 = new SqlDataAdapter();
myDataAdapter2.SelectCommand = command2;
// Adds rows in the DataSet
//DataSet myDataSet2 = new DataSet();
DataTable cobtable = new DataTable();
myDataAdapter2.Fill(cobtable);
DataColumn cl = cobtable.Columns.Add("Total");
cobtable.Columns["Total"].SetOrdinal(1);
DataRow dr;
dr = cobtable.NewRow();
dr["Categoria"] = "Total";
cobtable.Rows.InsertAt(dr, 0);
dr = cobtable.NewRow();
dr["Categoria"] = "";
cobtable.Rows.InsertAt(dr, 1);
dr = cobtable.NewRow();
dr["Categoria"] = "%";
cobtable.Rows.InsertAt(dr, 3);
dr = cobtable.NewRow();
dr["Categoria"] = "";
cobtable.Rows.InsertAt(dr, 4);
dr = cobtable.NewRow();
dr["Categoria"] = "%";
cobtable.Rows.InsertAt(dr, 6);
dr = cobtable.NewRow();
dr["Categoria"] = "";
cobtable.Rows.InsertAt(dr, 7);
dr = cobtable.NewRow();
dr["Categoria"] = "%";
cobtable.Rows.InsertAt(dr, 9);
GroupGrid.DataSource = cobtable;
GroupGrid.DataBind();
}
c

DataGrid View Not Showing Newly Added Record using Access Database

I am new to C# and I want to do this ASAP as I've had this problem since past week. I have tried a few approaches and not yet gotten the correct output. Here is my code:
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
String itemcode = tbItemCode.Text.ToString();
String shade = tbShade.Text.ToString();
String rollnumber = tbRollNumber.Text.ToString();
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Eranga\Documents\Visual Studio 2010\Projects\RollAllocationModel\RollAllocationModel\Roll.mdb;Persist Security Info=True;Jet OLEDB:Database Password=admin");
String deletequery = "DELETE FROM TabRoll WHERE (ItemCode = '" + itemcode + "') AND (Shade = '" + shade + "') AND (RollNumber = '" + rollnumber + "')";
//String deletequery = "SELECT * FROM TabRoll";
//code by query builder ----> DELETE FROM TabRoll WHERE (ItemCode = '" + itemcode + "') AND (Shade = '" + shade + "') AND (RollNumber = '" + length + "');
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(deletequery, conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
BindingSource bSource = new BindingSource();
bSource.DataSource = dt;
dataGridView1.EndEdit();
bSource.EndEdit();
da.Update(dt);
dataGridView1.DataSource = bSource;
conn.Close();
MessageBox.Show("Data deleted");
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
Why doesn't the DataGrid does not update with the new record.

How can i show multi checkbox on datagridview

i need show multi checkbox on datagridview.
i have 4 checkbox when select 2 checkbox it's show 2 checkbox on datagridview.
Ex 2 checkbox.
CheckBox missShw
0001
0002
CheckBox leaveFull
0003
0004
i'm First select CheckBox missShw and CheckBox leaveFull it's output.
0003
0004
OR
i'm First select CheckBox leaveFull and CheckBox missShw it's output.
0001
0002
i need output when 2 checkbox.
0001
0002
0003
0004
now, i'm select 2 checkbox, so it's show all data to datagridview but it's don't show all data.
This code:
public void missShw()
{
SqlConnection conn = new SqlConnection(appConn);
string sql = "SELECT [filesTA].EmpNo,[Employee].Title + ' ' + [Employee].[First Name] + ' ' + [Employee].[Last Name] as 'FullName',[filesTA].ChkDate"
+ ",Convert(nvarchar(5),[filesTA].ChkIn,108) as 'ChkIn',Convert(nvarchar(5),[filesTA].ChkOut,108) as 'ChkOut"
+ ",[filesTA].LateMin"
+ " From [WebSP].[dbo].[filesTA] inner join [WebSP].[dbo].[Employee] on [Employee].EmployeeNo=[filesTA].EmpNo INNER JOIN [WebSP].[dbo].[CompanyData] On [CompanyData].Company = [Employee].Company"
+ " WHERE [filesTA].ChkDate ='" + dateTimePicker.Value.ToString("yyyy-MM-dd") + "'"
+ " and [Employee].Section = '" + cbSection.SelectedValue + "'"
+ " and [Employee].Team = '" + cbTeam.SelectedValue + "'"
+ " and [filesTA].ErrorCode = '2'";
da = new SqlDataAdapter(sql, Conn);
DataSet ds = new DataSet();
da.Fill(ds);
Conn.Close();
dgvShow.DataSource = ds.Tables[0];
}
public void leaveFull()
{
SqlConnection conn = new SqlConnection(appConn);
string sql = "SELECT [filesTA].EmpNo,[Employee].Title + ' ' + [Employee].[First Name] + ' ' + [Employee].[Last Name] as 'FullName',[filesTA].ChkDate"
+ ",Convert(nvarchar(5),[filesTA].ChkIn,108) as 'ChkIn',Convert(nvarchar(5),[filesTA].ChkOut,108) as 'ChkOut"
+ ",[filesTA].LateMin"
+ " From [WebSP].[dbo].[filesTA] inner join [WebSP].[dbo].[Employee] on [Employee].EmployeeNo=[filesTA].EmpNo INNER JOIN [WebSP].[dbo].[CompanyData] On [CompanyData].Company = [Employee].Company"
+ " WHERE [filesTA].ChkDate ='" + dateTimePicker.Value.ToString("yyyy-MM-dd") + "'"
+ " and [Employee].Section = '" + cbSection.SelectedValue + "'"
+ " and [Employee].Team = '" + cbTeam.SelectedValue + "'"
+ " and [filesTA].ErrorCode = '3'";
da = new SqlDataAdapter(sql, Conn);
DataSet ds = new DataSet();
da.Fill(ds);
Conn.Close();
dgvShow.DataSource = ds.Tables[0];
}
//missShw()
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked == true)
{
missShw();
}
}
//leaveFull()
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked == true)
{
leaveFull();
}
}
Thanks for your time. :)

Categories

Resources