Small is New Big in C#

Coding

Archive for June 2010

How to create customer Timer

leave a comment »

C# Verstion:

        private DateTime dt = new DateTime(2010, 06, 30, 00, 00, 01, 00);
        private System.Timers.Timer timer = new System.Timers.Timer();
        private string nowtime;
        public delegate void TimeString(string timestr);

        public void UpdateTimeString(string str)
        {
            label1.Text = str.ToString();
        }

        private void TimeTicking(object sender, System.Timers.ElapsedEventArgs e)
        {
            dt = dt.AddSeconds(1.0);
            nowtime = dt.ToString("HH:mm:ss");
            object[] para = { nowtime };
            this.Invoke(new TimeString(UpdateTimeString), para);
        }

        private void TimeTick()
        {
            timer.Interval = 1000.0;
            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(TimeTicking);
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            this.TimeTick();
        }

VB.NET Version:

Private dt As New DateTime(2010, 6, 30, 0, 0, 1, 0)
Private timer As New System.Timers.Timer()
Private nowtime As String

Public Delegate Sub TimeString(timestr As String)

Public Sub UpdateTimeString(str As String)
label1.Text = str.ToString()
End Sub

Private Sub TimeTicking(sender As Object, e As System.Timers.ElapsedEventArgs)
dt = dt.AddSeconds(1.0)
nowtime = dt.ToString("HH:mm:ss")
Dim para As Object() = {nowtime}
Me.Invoke(New TimeString(AddressOf UpdateTimeString), para)
End Sub

Private Sub TimeTick()
timer.Interval = 1000.0
timer.Enabled = True
AddHandler timer.Elapsed, New ElapsedEventHandler(AddressOf TimeTicking)
End Sub

Private Sub Form3_Load(sender As Object, e As EventArgs)
Me.TimeTick()
End Sub


Advertisements

Written by nauhil

June 30, 2010 at 1:47 pm

Posted in Levels Of Details

Selecting Suggestion of Combobox

leave a comment »



private SqlConnection conn;

public SqlConnection GetSqlConn()
{
try
{
string strconn = "Server=local;Database=AdventureWorks;User id=sa;PWD=";
conn = new SqlConnection(strconn);
conn.Open();
return conn;
}
catch (Exception ex)
{
return null;
}
}


public void cboxBind(string sqlComm, string tableName, string tableColumn, ComboBox cbox)
{
DataSet ds = this.GetDataSet(sqlComm, tableName);
cbox.DataSource = ds.Tables[tableName];
cbox.DisplayMember = tableColumn;
}

public DataSet GetDataSet(string sqlComm, string tableName)
{
SqlConnection sqlconn = this.GetSqlConn();
SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
DataSet ds = new DataSet();
sqlda.Fill(ds, tableName);
return ds;
}


public Form1()
{
InitializeComponent();

DataSet ds = this.GetDataSet("SELECT Distinct Name FROM Person.CountryRegion","Person.CountryRegion");
string[] list = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
list[i] = ds.Tables[0].Rows[i][0].ToString();
}

comboBox1.AutoCompleteCustomSource.AddRange(list);
comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
}

private void Form1_Load(object sender, EventArgs e)
{
this.cboxBind("SELECT DISTINCT Name FROM Person.CountryRegion", "Person.CountryRegion","Name", comboBox1);

}

Written by nauhil

June 18, 2010 at 2:03 pm

Posted in Tips of C#

DataGridView numeric question

leave a comment »

How do you add numeric values and display these values in this
control.

EX: col 1 has $100.00, col 2 has $200.00, column 3 then displays the
total in the control which is calculated > $300.00.

My question is for horizontal calculation and vertical calculation
within the datagridview.

Also if possible provide binding answers and runtime answers
PLEASE!!!


I have written a sample of horizontal calculation to you <br/>
<br/>
dgvDepartment is datagridview<br/>
<br/>
<br/>
private void frmDepartManage_Load(object sender, EventArgs e)<br/>
{<br/>
<br/>
DataSet ds = this.GetDs("SELECT Bin,Quantity FROM Production.ProductInventory","Production.ProductInventory");//get your dataset<br/>
ds.Tables[0].Columns.Add("Total", typeof(<span style="color:#a31515">Decimal</span>
)); // add new column to calculate total<br/>
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)<br/>
{<br/>
ds.Tables[0].Rows[i][2] = Convert.ToDecimal(ds.Tables[0].Rows[i][0]) + Convert.ToDecimal(ds.Tables[0].Rows[i][1]);<br/>
}<br/>
<br/>
dgvDepartment.DataSource = ds.Tables[0];<br/>
}<br/>
<br/>
<br/>
//if total> 300.00 then displays<br/>
private void dgvDepartment_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)<br/>
{<br/>
string colName = this.dgvDepartment.Columns[e.ColumnIndex].Name.ToString();<br/>
if (colName == "Total")<br/>
{<br/>
<br/>
if (Convert.ToDecimal(e.Value) < 300.00)<br/>
{<br/>
<br/>
e.Value = "";<br/>
e.FormattingApplied = true;<br/>
<br/>
}<br/>
<br/>
}<br/>
<br/>
}

Written by nauhil

June 14, 2010 at 11:07 am

Posted in Q&A

How can i create frozen datagridview at RowCount – 1

leave a comment »

I have create an row Total at bottom DataGridview and I would like to
fix this Row always show in DataGridView.

Can I frozen this this Row (position: RowCount – 1) out of bug when
DataGridView have Scrolls?

Has Anyone got other way to always this row?

//I think you want add new row to datagridview's bottom, I have written a sample to you. 

private int sum;

private void frmMain_Load(object sender, EventArgs e)
{
DataSet ds = db.GetDataSet("select * from tb_InputGoods", "tb_InputGoods"); //Get your dataset

//--------- sum total --------//
DataRow dr = ds.Tables[0].NewRow();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sum = Convert.ToInt16(ds.Tables[0].Rows[i][8]) + sum;
}
dr[8] = sum;
ds.Tables[0].Rows.Add(dr);
//----------------------------//
dataGridView1.DataSource = ds.Tables[0];

}

Written by nauhil

June 14, 2010 at 11:05 am

Posted in Q&A

Datagridview refresh contents…

leave a comment »

hello, i have a datagridview and i add edit and delete
it’s values… and after every change i do
this.myTableAdapter.Fill(this.myDataSet.my);

this updates my datagrid successfully but if i had a row
selected before update in the datagridview after the update no row is
selected and it goes to top (if there is a scrollbar)… but i don’t
want to lose my selection and i want to stay at current position…

    namespace program
{
public partial class frmMain : Form
{
public int focusRow;
public bool boolRow;

private void frmMain_Load(object sender, EventArgs e)
{
//bound your data to datagirdview
{ code }

if(boolRow)
{
datagridview.FirstDisplayedScrollingRowIndex = focusRow;
datagridview.Rows[focusRow].Selected = true;
}
}

private void buttonEdit_Click(object sender, EventArgs e)
{
//your edit and update database code
{ code }

focusRow = datagridview.CurrentRow.Index;

boolRow = true;

frmMain_Load(sender,e);
}

}
}

Written by nauhil

June 14, 2010 at 10:56 am

Posted in Q&A

Crytal report print preview

leave a comment »

public SqlConnection getcon()
{
string M_str_sqlcon = "Server=172.18.39.48;Database=ApressFinancial;User id=sa;PWD=";
SqlConnection mycon = new SqlConnection(M_str_sqlcon);
mycon.Open();
return mycon;
}

public DataSet getds(string M_str_sqlstr, string M_str_table)
{
SqlConnection sqlcon = this.getcon();
SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlda.Fill(myds, M_str_table);
return myds;
}

public ReportDocument DailyCrystalReports(string creportName, string sum_sql, System.Data.DataTable tableName)
{
string P_str_creportPath = System.Windows.Forms.Application.StartupPath.Substring(0, System.Windows.Forms.Application.StartupPath.Substring(0, System.Windows.Forms.Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
P_str_creportPath += @"\DailyHB\CrReport\" + creportName; //Your report path
ReportDocument doc = new ReportDocument();
doc.Load(P_str_creportPath);
doc.SetDataSource(tableName);
return doc;
}

private void frmDailyCenterIssue_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.Sizable;

string sum_sql = "SELECT * FROM CenterIssueSummary WHERE SummaryDate = '" + frmSummaryCenterIssue.daily + "' Order By CenterNo"; //Change your sql
DataSet ds = this.GetDs(sum_sql, "CenterIssueSummary"); //Create DataSet
DataTable dt = ds.Tables[0]; //Get Table

crystalReportDaily.ReportSource = this.DailyCrystalReports("SummaryCenterIssue.rpt", sum_sql, sumDt);
}

Written by nauhil

June 13, 2010 at 10:57 pm

Posted in Tips of C#