Hi. I am using a DataGridView binded to a DataSet.
dataGridView1.DataSource = dataSet1.Tables[0].DefaultView;
I am able to format how data will be shown in the columns by using "DefaultCellStyle.Format" property.
dataGridView1.Columns["colName" ].DefaultCellStyle.Format = "Product - 0" ;
But, i have to manage this in a conditional case of the data.
For example; If data is "0" in the cell, it must be displayed as "Stock was added". If data is "1", then it must be displayed as "Stok was removed". How can i do that?
I was adding the data into dataGridView row by row in a loop. So i was able to process each row before adding into dataGridView. But adding rows one by one makes a lot of performance problems. I've a searchBox in the Form, and TextChanged event connects to database and refreshes dataGridView. And when the returned row count is really high, it works slowly. So, i have to solve this by some column properties on dataGridView. - Edited bynoldor Tuesday, August 25, 2009 11:16 PM
- Edited bynoldor Tuesday, August 25, 2009 11:16 PM
- Edited bynoldor Tuesday, August 25, 2009 11:17 PM
- Edited bynoldor Tuesday, August 25, 2009 11:20 PM
- Moved byDavid M MortonMVP, ModeratorSaturday, August 29, 2009 7:48 PMMore a Windows Forms question than C# General (From:Visual C# General)
-
| | noldor Tuesday, August 25, 2009 11:12 PM | Hi, Using CellFormatting eventcan do the job void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // e.Value= //change the value according to it's original value } You might also want to use a procedure to change the value of this field when retrieving data , this would be faster. Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Marked As Answer bynoldor Saturday, August 29, 2009 7:00 PM
-
| | Harry Zhu Wednesday, August 26, 2009 9:01 AM | Here is some sample code with a quick explanation. In the sample there are two DataTables. The first DataTable, named stockTable, has a column containing the text you want to display in a datagrid column, "Stock was added" or "Stock was removed", and it's associated code, 0 or 1. There is also a DataTable which contains all of the data you want to display in the DataGridView called gridDT. You need to create a DataGridViewComboBoxColumn for the DataGridView and give the column a data source so it can lookup the display value. In this sample we use gridDT. You also need to tell the DataGridViewComboBoxColumn which column in gridDT to display, col1.DisplayMember = "display";, and which is the value to map to the data in the DataGridView, col1.ValueMember = "value";. Finally you need to tell the DataGridViewComboBoxColumn which column in gridDT contains the data that col1.ValueMember will be maped to. In this case col1.DataPropertyName = "Column1". private void Form1_Load(object sender, EventArgs e) { // create the table with the data value in the table(0 or 1) and the value we want // to display ('Stock was added' or 'Stock was removed') DataTable stockTable = new DataTable(); DataColumn dc = stockTable.Columns.Add("value"); stockTable.Columns.Add("display"); stockTable.Rows.Add(new object[] { 0, "Stock was added" }); stockTable.Rows.Add(new object[] { 1, "Stock was removed" }); // **************************************************** // Create the ComboBox column for the DataGridView // **************************************************** System.Windows.Forms.DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn(); col1.DataSource = stockTable; // the table with the data to display in the column col1.HeaderText ="Column1"; // Column name in grid display col1.DisplayMember = "display"; // column name from stockTable col1.ValueMember = "value"; // column name from stockTable col1.DataPropertyName = "Column1"; // column name from gridDT dataGridView.Columns.Add(col1); // ADD COLUMN TO DATAGRIDVIEW DataTable gridDT = new DataTable("testPrint"); DataColumn col = gridDT.Columns.Add("Column1"); gridDT.Columns.Add("Column2"); gridDT.Columns.Add("Column3"); gridDT.Columns.Add("Column4"); gridDT.Columns.Add("Column5"); gridDT.Rows.Add(new object[] { "1", "Value2", "Value3", "Value4", "Value5" }); gridDT.Rows.Add(new object[] { "0", "Value22", "Value32", "Value42", "Value52" }); gridDT.Rows.Add(new object[] { "0", "Value23", "Value33", "Value43", "Value53" }); gridDT.Rows.Add(new object[] { "1", "Value24", "Value34", "Value44", "Value54" }); gridDT.Rows.Add(new object[] { "1", "Value25", "Value35", "Value45", "Value55" }); gridDT.Rows.Add(new object[] { "0", "Value26", "Value36", "Value46", "Value56" }); dataGridView.DataSource = gridDT; }
- Marked As Answer bynoldor Saturday, August 29, 2009 7:00 PM
-
| | bucman6 Wednesday, August 26, 2009 2:27 AM | Thanks for your replies. And apologize for my lately answer. Your messages are both seems to be a good solution. But i've chosen solving it in my sql statement to achieve the fastest result, just as Harry Zhu pointed. Something like; SELECT bla,bla,bla,bla, (CASE WHEN (stockJob=0) THEN 'stock added' WHEN (stockJob=1) THEN 'stock removed' ELSE 'N/A' END) as stkMsg FROM BLA INNER JOIN BLABLABA ON BLABLABLABLA - Marked As Answer bynoldor Saturday, August 29, 2009 7:00 PM
-
| | noldor Saturday, August 29, 2009 6:59 PM | Here is some sample code with a quick explanation. In the sample there are two DataTables. The first DataTable, named stockTable, has a column containing the text you want to display in a datagrid column, "Stock was added" or "Stock was removed", and it's associated code, 0 or 1. There is also a DataTable which contains all of the data you want to display in the DataGridView called gridDT. You need to create a DataGridViewComboBoxColumn for the DataGridView and give the column a data source so it can lookup the display value. In this sample we use gridDT. You also need to tell the DataGridViewComboBoxColumn which column in gridDT to display, col1.DisplayMember = "display";, and which is the value to map to the data in the DataGridView, col1.ValueMember = "value";. Finally you need to tell the DataGridViewComboBoxColumn which column in gridDT contains the data that col1.ValueMember will be maped to. In this case col1.DataPropertyName = "Column1". private void Form1_Load(object sender, EventArgs e) { // create the table with the data value in the table(0 or 1) and the value we want // to display ('Stock was added' or 'Stock was removed') DataTable stockTable = new DataTable(); DataColumn dc = stockTable.Columns.Add("value"); stockTable.Columns.Add("display"); stockTable.Rows.Add(new object[] { 0, "Stock was added" }); stockTable.Rows.Add(new object[] { 1, "Stock was removed" }); // **************************************************** // Create the ComboBox column for the DataGridView // **************************************************** System.Windows.Forms.DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn(); col1.DataSource = stockTable; // the table with the data to display in the column col1.HeaderText ="Column1"; // Column name in grid display col1.DisplayMember = "display"; // column name from stockTable col1.ValueMember = "value"; // column name from stockTable col1.DataPropertyName = "Column1"; // column name from gridDT dataGridView.Columns.Add(col1); // ADD COLUMN TO DATAGRIDVIEW DataTable gridDT = new DataTable("testPrint"); DataColumn col = gridDT.Columns.Add("Column1"); gridDT.Columns.Add("Column2"); gridDT.Columns.Add("Column3"); gridDT.Columns.Add("Column4"); gridDT.Columns.Add("Column5"); gridDT.Rows.Add(new object[] { "1", "Value2", "Value3", "Value4", "Value5" }); gridDT.Rows.Add(new object[] { "0", "Value22", "Value32", "Value42", "Value52" }); gridDT.Rows.Add(new object[] { "0", "Value23", "Value33", "Value43", "Value53" }); gridDT.Rows.Add(new object[] { "1", "Value24", "Value34", "Value44", "Value54" }); gridDT.Rows.Add(new object[] { "1", "Value25", "Value35", "Value45", "Value55" }); gridDT.Rows.Add(new object[] { "0", "Value26", "Value36", "Value46", "Value56" }); dataGridView.DataSource = gridDT; }
- Marked As Answer bynoldor Saturday, August 29, 2009 7:00 PM
-
| | bucman6 Wednesday, August 26, 2009 2:27 AM | Hi, Using CellFormatting eventcan do the job void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // e.Value= //change the value according to it's original value } You might also want to use a procedure to change the value of this field when retrieving data , this would be faster. Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Marked As Answer bynoldor Saturday, August 29, 2009 7:00 PM
-
| | Harry Zhu Wednesday, August 26, 2009 9:01 AM | Thanks for your replies. And apologize for my lately answer. Your messages are both seems to be a good solution. But i've chosen solving it in my sql statement to achieve the fastest result, just as Harry Zhu pointed. Something like; SELECT bla,bla,bla,bla, (CASE WHEN (stockJob=0) THEN 'stock added' WHEN (stockJob=1) THEN 'stock removed' ELSE 'N/A' END) as stkMsg FROM BLA INNER JOIN BLABLABA ON BLABLABLABLA - Marked As Answer bynoldor Saturday, August 29, 2009 7:00 PM
-
| | noldor Saturday, August 29, 2009 6:59 PM |
|