I want my checkbox to be visible as button. Check-boxes are generated dynamically in code behind file using C#.
How can I do that?
private void loadItems()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "ConString";
con.Open();
SqlCommand cmd = new SqlCommand("Select ItemId,ItemName from ITEMS", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
iid = (dt.Rows[i]["ItemId"]).ToString();
iname = dt.Rows[i]["ItemName"].ToString();
CheckBox btn = new CheckBox();
btn.Content = iname;
btn.Height = 20;
btn.Width = 150;
btn.Tag = iname;
//var2 = btn.Tag.ToString();
wrapItems.Children.Add(btn);
btn.Click += new RoutedEventHandler(CheckBoxChecked);
//btn.RaiseEvent(new RoutedEventArgs(Button.btnClick(sender,e)));
con.Close();
}
Related
I am new to SharePoint programming.
Can anyone tell me how I can export list data to Excel using a timer job with some custom code?
Please go through the below link for creating timer jobs in sharepoint.
This article is contain the detailed process.
https://www.mssqltips.com/sqlservertip/3801/custom-sharepoint-timer-job/
The below code will help you to export the list data.
public void Export(List<int> ids)
{
DataTable table = new DataTable();
try
{
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteE = new SPSite(site.ID))
{
using (SPWeb webE = siteE.OpenWeb(web.ID))
{
webE.AllowUnsafeUpdates = true;
SPList list = webE.Lists["Stationery"];
table.Columns.Add("Product", typeof(string));
table.Columns.Add("Quantity", typeof(Decimal));
DataRow newRow;
GridView gv = new GridView();
foreach (SPListItem item in list.Items)
{
if (ids.Contains(Convert.ToInt32(item["ID"].ToString())) && (item["Status"].ToString() == "New"))
{
newRow = table.Rows.Add();
newRow["Product"] = item["Product"].ToString();
newRow["Quantity"] = Convert.ToDecimal(item["Quantity"].ToString());
item["Status"] = "Exported";
item.Update();
}
}
SPBoundField boundField = new SPBoundField();
boundField.HeaderText = "Product";
boundField.DataField = "Product";
gv.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "Quantity";
boundField.DataField = "Quantity";
boundField.ControlStyle.Width = new Unit(120);
gv.Columns.Add(boundField);
gv.AutoGenerateColumns = false;
gv.DataSource = table.DefaultView;
gv.DataBind();
gv.AllowSorting = false;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
string attachment = "attachment; filename=export" + "_" + DateTime.Now.ToShortTimeString() + ".xls";
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/Excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
webE.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HttpContext.Current.Response.Write(ex.ToString());
}
When i replace EmerPacienti with PatientId it works fine, why happen that?
if (DropDownList3.SelectedItem.Value == "In-Pacient")
{
SqlDataAdapter Da = new SqlDataAdapter("select * from Ipacient where EmerPacienti=" + TextBox11.Text + "", cn);
//SqlCommandBuilder Cmd = new SqlCommandBuilder(Da);
DataSet Ds = new DataSet();
Da.Fill(Ds, "Ipacient");
GridView1.DataSource = Ds.Tables[0];
GridView1.DataBind();
Use sql parameters to prevent sql injection, then you also don't need to wrap it in apostrophes which is required on text columns. So this is much better:
using (var cn = new SqlConnection("insert connection string"))
using(var da = new SqlDataAdapter("select * from Ipacient where EmerPacienti=#EmerPacienti", cn))
{
da.SelectCommand.Parameters.Add("#EmerPacienti", SqlDbType.VarChar).Value = TextBox11.Text;
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
If it was an int-value use int.Parse/int.TryParse:
using (var cn = new SqlConnection("insert connection string"))
using(var da = new SqlDataAdapter("select * from Ipacient where PatientId=#PatientId", cn))
{
int patientId;
if (int.TryParse(TextBox11.Text, out patientId))
{
da.SelectCommand.Parameters.Add("#PatientId", SqlDbType.Int).Value = patientId;
// ...
}
}
i am getting error on sda.Fill(dt); that Fill:selectcommand.connction property has not been initialised what could be a problem in connection?
protected void Updatecustomer(object sender, GridViewUpdateEventArgs e)
{
string cstid = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblcustomerid")).Text;
string csnm = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname")).Text;
string cmpn = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcompany")).Text;
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\LENOVO\\Documents\\Visual Studio 2010\\WebSites\\LiveSms\\App_Data\\Sms.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update gridCompany set name=#name,company=#company where cid=#cid;
cmd.Parameters.Add("#cid", SqlDbType.NVarChar).Value = cstid;
cmd.Parameters.Add("#name", SqlDbType.NVarChar).Value = csnm;
cmd.Parameters.Add("#comnpany", SqlDbType.NVarChar).Value = cmpn;
GridView1.EditIndex = -1;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
I am exporting data from sql server database to excel in wpf, and I have achieved the function successully. Now I want to change the head column colour in the generated excel. Any ideas? Thanks in advance.
private void button1_Click(object sender, RoutedEventArgs e)
{
string sql = null;
string data = null;
// string path = null;
//string myfilename = "Report";
int i = 0;
int j = 0;
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
//xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Customer List";
//connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;";
//SqlConnection cnn = new SqlConnection(GetConnectionString());
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = #"Data Source=.\sqlexpress;Initial Catalog=ClientLists;Integrated Security=SSPI;";
cnn.Open();
sql = "select FirstName, LastName, City, PostCode, TelephoneNo from Customers";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count -1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count -1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
Microsoft.Office.Interop.Excel.Range headerRange1 = xlWorkSheet.get_Range("A1", "A1");
headerRange1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange1.Value = "First Name";
headerRange1.Font.Bold = true;
headerRange1.ColumnWidth = 14;
// headerRange1.Interior.Color = 1;
// headerRange1.Borders.Color = System.Drawing.Color.Red;
Microsoft.Office.Interop.Excel.Range headerRange2 = xlWorkSheet.get_Range("B1", "B1");
headerRange2.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange2.Value = "Last Name";
headerRange2.Font.Bold = true;
headerRange2.ColumnWidth = 14;
Microsoft.Office.Interop.Excel.Range headerRange3 = xlWorkSheet.get_Range("C1", "C1");
headerRange3.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange3.Value = "City";
headerRange3.Font.Bold = true;
headerRange3.ColumnWidth = 14;
Microsoft.Office.Interop.Excel.Range headerRange4 = xlWorkSheet.get_Range("D1", "D1");
headerRange4.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange4.Value = "Post Code";
headerRange4.Font.Bold = true;
headerRange4.ColumnWidth = 14;
Microsoft.Office.Interop.Excel.Range headerRange5 = xlWorkSheet.get_Range("E1", "E1");
headerRange5.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange5.Value = "Telephone NO";
headerRange5.Font.Bold = true;
headerRange5.ColumnWidth = 14;
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
There is a post that can be found here that says that this is not possible.
However, the c# excel how to change a color of a particular row post on StackOverflow that provides code for changing a row colour... you may be able to adapt it for you purposes.
Add New Row in GridView winform on Button Click.
Need to have single button in grid rather than DataGridViewButtonColumn
you can add it by using
GridView1.DataSource = datatable;
and then add new row by this
datatable.Rows.Add();
this will add new row and give control id into add().
use this
public partial class Form1 : Form
{
Button textBoxDgv1 = new Button();
Label labelDgv1 = new Label();
the next is on the Form_Load event
private void Form1_Load(object sender, EventArgs e)
{
labelDgv1.Text = "Delete";
labelDgv1.Height = 20;
labelDgv1.AutoSize = false;
labelDgv1.BorderStyle = BorderStyle.FixedSingle;
labelDgv1.TextAlign = ContentAlignment.MiddleCenter;
int Xdgv1 = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgv1;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
this.dataGridView1.Controls.Add(labelDgv1);
and one more section is in dataGridView1_CellPainting
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sum = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[3].Value != string.Empty)
{
sum += Convert.ToInt32(this.dataGridView1[3, i].Value);
}
}
textBoxDgv1.Text = sum.ToString();
int Xdgvx = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgvx;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
textBoxDgv1.Width = this.dataGridView1.Columns[3].Width;
Xdgvx = this.dataGridView1.GetCellDisplayRectangle(3, -1, true).Location.X;
textBoxDgv1.Location = new Point(Xdgvx, this.dataGridView1.Height - textBoxDgv1.Height);
}