C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Datagridview unbound columns


Posted Date: 03 Sep 2008      Total Responses: 2

Posted By: Krivahn       Member Level: Bronze     Points: 1


I have a gridview that has bound & unbound virtual columns.
In the bound columns I get the data to the datagridview. With the unbound columns I want to add to a Dictionary. How do I do this ?
Code follows :

//create a new dataset
DataSet dataSet = new DataSet();

SqlCommand selectCommand = new SqlCommand("sp_GetTerminalType", sqlConn);
//SELECTion via a stored procedure
selectCommand.CommandType = CommandType.StoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = selectCommand;
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
//bindingSource.DataSource = dataTable;

dataGridView.DataSource = dataTable;

dataGridView.Columns["TrmT_ID_SI"].Visible = false;
dataGridView.Columns[1].HeaderText = "Terminal Name";
dataGridView.Columns[1].MinimumWidth = 85;


//unbound columns
// The check box(unbound) columns will be virtual.
dataGridView.VirtualMode = true;

DataGridViewCheckBoxColumn TimeAttendance = new DataGridViewCheckBoxColumn(false);
TimeAttendance.HeaderText = "Time And Attendance";
dataGridView.Columns.Insert(2, TimeAttendance);
dataGridView.Columns[2].MinimumWidth = 50;
dataGridView.Columns[2].Name = "TimeAttendance";


DataGridViewCheckBoxColumn Access = new DataGridViewCheckBoxColumn(false);
Access.HeaderText = "Access";
dataGridView.Columns.Insert(3, Access);
dataGridView.Columns[3].MinimumWidth = 40;
dataGridView.Columns[3].Name = "Access";

// Initialize the dictionary that contains the boolean check state.
checkState = new Dictionary<int, bool>();


private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// Update the status bar when the cell value changes.
if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
// Get the orderID from the OrderID column.
int TimeAttendanceID = (int)dataGridView.Rows[e.RowIndex].Cells["TimeAttendance"].Value;
checkState[TimeAttendanceID] = (bool)dataGridView.Rows[e.RowIndex].Cells[0].Value;

}
}

private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
// Handle the notification that the value for a cell in the virtual column
// is needed. Get the value from the dictionary if the key exists.

if (e.ColumnIndex == 2)
{
int TimeAttendanceID = (int)dataGridView.Rows[e.RowIndex].Cells["TimeAttendance"].Value;
if (checkState.ContainsKey(TimeAttendanceID))
{
e.Value = checkState[TimeAttendanceID];
}
else
e.Value = false;
}





}

private void dataGridView_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
if (e.ColumnIndex == 0)
{
// Get the orderID from the OrderID column.
int TimeAttendanceID = (int)dataGridView.Rows[e.RowIndex].Cells["TimeAttendance"].Value;

// Add or update the checked value to the dictionary depending on if the
// key (orderID) already exists.
if (!checkState.ContainsKey(TimeAttendanceID))
{
checkState.Add(TimeAttendanceID, (bool)e.Value);
}
else
checkState[TimeAttendanceID] = (bool)e.Value;
}

}




Responses

Author: vishal     04 Sep 2008Member Level: GoldRating:     Points: 2
create a loop for number of items present in dictionary and set the value for each cell within the look or you can write on grid initialize method.


Author: Krivahn    05 Sep 2008Member Level: BronzeRating:     Points: 1
Thx......
Any example on how to do this ?


Post Reply
You must Sign In to post a response.
Next : datagridview problem
Previous : How to create setup file which can.......
Return to Discussion Forum
Post New Message
Category: Windows

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

SPOC

Contact Us    Privacy Policy    Terms Of Use