Small is New Big in C#

Coding

Archive for the ‘Q&A’ Category

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