Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace GridView { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { //Make new datatable DataTable odt = new DataTable(); //add datacolumns in datatable DataColumn odc = new DataColumn(); DataColumn odcSec = new DataColumn(); DataColumn odcThird = new DataColumn(); DataColumn odcForth = new DataColumn(); DataColumn odcFifth = new DataColumn(); DataColumn odcSixth = new DataColumn(); odc.ColumnName = "Class"; odt.Columns.Add(odc); odcSec.ColumnName = "Subject Name"; odt.Columns.Add(odcSec); odcThird.ColumnName = "Grade"; odt.Columns.Add(odcThird); odcForth.ColumnName = "GradeCollection"; odt.Columns.Add(odcForth); odcFifth.ColumnName = "Pass"; odt.Columns.Add(odcFifth); odcSixth.ColumnName = "Fail"; odt.Columns.Add(odcSixth); //make new rows for datatable DataRow odr = odt.NewRow(); odr["Class"] = "FYBA"; odr["Subject Name"] = "Hindi"; odr["Grade"] = "A"; odr["GradeCollection"] = "A,B,C,D,E"; odr["Pass"] = true; odr["Fail"] = false; odt.Rows.Add(odr); DataRow odrFirst = odt.NewRow(); odrFirst["Class"] = "SYBA"; odrFirst["Subject Name"] = "English"; odrFirst["Grade"] = "B"; odrFirst["GradeCollection"] = "A,B,C,D,E"; odrFirst["Pass"] = true; odrFirst["Fail"] = false; odt.Rows.Add(odrFirst);

DataRow odrSecond = odt.NewRow(); odrSecond["Class"] = "SYBA"; odrSecond["Subject Name"] = "English"; odrSecond["Grade"] = "C"; odrSecond["GradeCollection"] = "A,B,C,D,E"; odrSecond["Pass"] = false; odrSecond["Fail"] = true; odt.Rows.Add(odrSecond); DataRow odrThird = odt.NewRow(); odrThird["Class"] = "TYBA"; odrThird["Subject Name"] = "Marathi"; odrThird["Grade"] = "D"; odrThird["GradeCollection"] = "A,B,C,D,E"; odrThird["Pass"] = true; odrThird["Fail"] = false; odt.Rows.Add(odrThird); DataRow odrForth = odt.NewRow(); odrForth["Class"] = "FYBA"; odrForth["Subject Name"] = "Telagu"; odrForth["Grade"] = "E"; odrForth["GradeCollection"] = "A,B,C,D,E"; odrForth["Pass"] = false; odrForth["Fail"] = true; odt.Rows.Add(odrForth); //Here You can take Your own connection string and fill datatable fr om database if (odt != null && odt.Rows.Count > 0) { //add datagridview textbox column in datagridview DataGridViewTextBoxColumn txtClass = new DataGridViewTextBoxColu mn(); txtClass.HeaderText = "Class"; txtClass.MaxInputLength = 20; txtClass.Width = 70; txtClass.Name = "Class"; kryptonDataGridView1.Columns.Add(txtClass); //add another gridview textbox column in datagridview DataGridViewTextBoxColumn txtSubjectName = new DataGridViewTextB oxColumn(); txtSubjectName.HeaderText = "SubjectName"; txtSubjectName.MaxInputLength = 20; txtSubjectName.Width = 70; txtSubjectName.Name = "SubjectName"; kryptonDataGridView1.Columns.Add(txtSubjectName); //add comboboxcolumn in datagridview DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComb oBoxColumn(); comboboxColumn.HeaderText = "Grade"; comboboxColumn.DropDownWidth = 160; comboboxColumn.Width = 90; comboboxColumn.MaxDropDownItems = 3; comboboxColumn.FlatStyle = FlatStyle.Flat; kryptonDataGridView1.TabStop = true; kryptonDataGridView1.Focus(); kryptonDataGridView1.Columns.Insert(2, comboboxColumn);

//add checkboxcolumn in datagridview DataGridViewCheckBoxColumn chkPass = new DataGridViewCheckBoxCol umn(); chkPass.HeaderText = "Pass"; chkPass.Name = "Pass"; chkPass.Width = 50; kryptonDataGridView1.Columns.Add(chkPass); //add another checkbox column in datagridview column DataGridViewCheckBoxColumn chkFail = new DataGridViewCheckBoxCol umn(); chkFail.HeaderText = "Fail"; chkFail.Name = "Fail"; chkFail.Width = 70; kryptonDataGridView1.Columns.Add(chkFail); for (int i = 0; i < odt.Rows.Count; ++i) { //Logic for filling combobox string[] row1 = new string[] { odt.Rows[i]["Class"].ToString (), odt.Rows[i]["Subject Name"].ToString() }; kryptonDataGridView1.Rows.Add(row1); string sItemNames = odt.Rows[i]["GradeCollection"].ToString( ); char[] charArray = new char[] { ',' }; string[] sItemNameArray = sItemNames.Split(charArray, String SplitOptions.RemoveEmptyEntries); DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Name"); dt.Columns.Add(dc1); DataRow odrinner = dt.NewRow(); odrinner["Name"] = "--Select--"; dt.Rows.Add(odrinner); foreach (string item in sItemNameArray) { dt.Rows.Add(item); } comboboxColumn.DataSource = dt; comboboxColumn.DisplayMember = "Name"; if (!string.IsNullOrEmpty(odt.Rows[i]["Grade"].ToString())) { kryptonDataGridView1.Rows[i].Cells[2].Value = odt.Rows[i ]["Grade"].ToString(); } else { comboboxColumn.DefaultCellStyle.NullValue = "--Select--" ; } //Fill checkbox column from datatable if (odt.Rows[i]["Pass"].ToString().Equals("False")) { kryptonDataGridView1.Rows[i].Cells["Pass"].Value = false ; } else

{ kryptonDataGridView1.Rows[i].Cells["Pass"].Value = true; } if (odt.Rows[i]["Fail"].ToString().Equals("False")) { kryptonDataGridView1.Rows[i].Cells["Fail"].Value = false ; } else { kryptonDataGridView1.Rows[i].Cells["Fail"].Value = true; } } } } //--------------------------------------------------private void kryptonDataGridView1_CellBeginEdit(object sender, DataGridViewCellC ancelEventArgs e) { if ((bool)kryptonDataGridView1.Rows[e.RowIndex].Cells[3].Value) { kryptonDataGridView1.Rows[e.RowIndex].Cells["Pass"].Value = kryptonDataGridView1.Rows[e.RowIndex].Cells["Fail"].Value = } if ((bool)kryptonDataGridView1.Rows[e.RowIndex].Cells[4].Value) { kryptonDataGridView1.Rows[e.RowIndex].Cells["Pass"].Value = kryptonDataGridView1.Rows[e.RowIndex].Cells["Fail"].Value = } } private void kryptonDataGridView1_CellContentClick(object sender, DataGridViewCe llEventArgs e) { if (kryptonDataGridView1.CurrentCell.ColumnIndex == 3) { if (kryptonDataGridView1.CurrentCell.Value != null) { kryptonDataGridView1.BeginEdit(true); bool checkState = (bool)kryptonDataGridView1.CurrentCell.Value; if (!checkState) { kryptonDataGridView1.Rows[e.RowIndex].Cells["Pass"].Value = true ; kryptonDataGridView1.Rows[e.RowIndex].Cells["Fail"].Value = fals e; return; } } } if (kryptonDataGridView1.CurrentCell.ColumnIndex == 4) { if (kryptonDataGridView1.CurrentCell.Value != null) {

true; false;

false; true;

kryptonDataGridView1.BeginEdit(true); bool checkState = (bool)kryptonDataGridView1.CurrentCell.Value; if (!checkState) { kryptonDataGridView1.Rows[e.RowIndex].Cells["Pass"].Value = fals e; kryptonDataGridView1.Rows[e.RowIndex].Cells["Fail"].Value = true ; return; } } } } //--------------------------------------private void kryptonDataGridView1_CellEnter(object sender, DataGridViewCellEvent Args e) { if (e.ColumnIndex == 2) { kryptonDataGridView1.BeginEdit(true); } }

You might also like