Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataTable Acts As Intermediator For DataGridView
 

DataTable Acts As Intermediator For DataGridView

I have one DataGridViewand i have defined the columns for this datagridview but i don't specify the DataSource for this DataGridView. As a result, it is just a merely DataGridView with columns.
 
After that, i insert a data by using DataGridView1.Items(0,0).Value="data" and however one of the columns is check box column, if the user tick on it, another row will be created.
 
I tried to put this DataGridView1.AllowUserToAddRow=False after the code of inserting the data then the data cannot be added. No matter how i dont want new row to be generated so i think of using DataTable acts an intermediate table and set it as DataGridView1.DataSource. My problem is i have no idea how to specify value for a row of a column for DataTable like what i did on DataGridView. Thank you.
lucerias  Thursday, February 01, 2007 6:43 AM

Hi As you mensioned above;

I create a demo and seems works well, hope it help you.

System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Text;

System.Windows.Forms;

WindowsApplication1

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

DataTable TB = new DataTable();

TB.Columns.Add("checked", typeof(bool));

TB.Columns.Add("name", typeof(String));

for (int i = 0; i <= 5; i++)

{

DataRow row = TB.NewRow();

if (i % 2==1)

row["checked"] = false;

else

row["checked"] = true;

row["name"] = "Name" + " " + i.ToString();

TB.Rows.Add(row);

}

this.dataGridView1.DataSource = TB.DefaultView;

dataGridView1.Columns.Clear();

DataGridViewCheckBoxColumn CC = new DataGridViewCheckBoxColumn();

CC.DataPropertyName = "checked";

DataGridViewTextBoxColumn TC = new DataGridViewTextBoxColumn();

TC.DataPropertyName = "name";

this.dataGridView1.Columns.Add(CC);

this.dataGridView1.Columns.Add(TC);

dataGridView1.AllowUserToAddRows = false;

}

}

Add two events as follow:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

if (e.ColumnIndex == 0)

{

string a = this.dataGridView1.CurrentCell.Value.ToString();

MessageBox.Show(a);

}

}

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

hope it help you

Bob zhu - SJTU  Friday, February 02, 2007 4:47 AM

Hi:

this one make the datagidview accept changes form checked box directlly when it received info,

I think without this code, actually the value of DGV cell wouldn't until validtion event coming 

and by the way, your tool convert c# to VB is so cool:)

Best regards!

Bob zhu - SJTU  Friday, February 02, 2007 7:12 AM

Hi As you mensioned above;

I create a demo and seems works well, hope it help you.

System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Text;

System.Windows.Forms;

WindowsApplication1

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

DataTable TB = new DataTable();

TB.Columns.Add("checked", typeof(bool));

TB.Columns.Add("name", typeof(String));

for (int i = 0; i <= 5; i++)

{

DataRow row = TB.NewRow();

if (i % 2==1)

row["checked"] = false;

else

row["checked"] = true;

row["name"] = "Name" + " " + i.ToString();

TB.Rows.Add(row);

}

this.dataGridView1.DataSource = TB.DefaultView;

dataGridView1.Columns.Clear();

DataGridViewCheckBoxColumn CC = new DataGridViewCheckBoxColumn();

CC.DataPropertyName = "checked";

DataGridViewTextBoxColumn TC = new DataGridViewTextBoxColumn();

TC.DataPropertyName = "name";

this.dataGridView1.Columns.Add(CC);

this.dataGridView1.Columns.Add(TC);

dataGridView1.AllowUserToAddRows = false;

}

}

Your code works, i have successfully passed the data to DataTable but i can't bound the data to the DataGridColumn, it seems quite messy because the Check value displayed as "True" or "False" in my Name DataGridTextColumn and my Check Column is empty without any data. May i know why? I have gone through the code for several times, the codes is shown as the following and there is no error for the bound. Thank you.
 
I realized that the problem might be caused by the checkbox, if i remove the DataProperty of the CheckBox then it works fine. Can i know how to tick on and off the CheckBox based on the DataTable check data? How to set the dataproperty of the checkColumn equals to a set of true of false values stored in database or datatable and tick on and off according to those values? Thank you.
 

Dim i As Integer

Dim DT As New DataTable

Dim DRow As DataRow

DT.Columns.Add("name", GetType(String))

DT.Columns.Add("check", GetType(Boolean))

For i = 0 To 5

DRow = DT.NewRow

DRow("name") = "name" + i.ToString

If i = 1 Then

DRow("check") = False

Else

DRow("check") = True

End If

DT.Rows.Add(DRow)

Next i

 

DataGridView1.DataSource = DT.DefaultView

DataGridView1.Columns.Clear()

Dim DGVTextColumn1 As New DataGridViewTextBoxColumn

DGVTextColumn1.Name = "Name"

DGVTextColumn1.DataPropertyName = "name"

DGVTextColumn1.Width = "100"

DataGridView1.Columns.Insert(0, DGVTextColumn1)

 

Dim DGVLinkColumn As New DataGridViewTextBoxColumn

DGVLinkColumn.Name = "Check"

DGVTextColumn1.DataPropertyName = "check"

DGVLinkColumn.Width = "80"

DGVLinkColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

DataGridView1.Columns.Insert(1, DGVLinkColumn)

 

lucerias  Thursday, February 01, 2007 9:36 AM
I have sucessfully solved the True or False Data Passing CheckBoxColumn but this lead to another problem. I assigned an action under DataGridView1_CellContentClick and check whether the checkbox is true or false. If it is true then nothing happens else a dialogbox will be displayed. However, when i tick off a parficular column and on again, the dialogbox is not showed. The dialogbox is showed only when i tick off this column, select any other row and on the previous checkbox again. I believe this may have something to do with update on the cell, update may only be performed when the selection on cell is false because of the default datagridview properties. May i know how to solve this? Thank you.
lucerias  Friday, February 02, 2007 4:06 AM

hi luceria:

as I use bind true or false to checkedcolumn the checked box show checked and unchecked;

in my code I just set checked box column's dataproperity to the boolean column in the datatable.

then the binding works

Bob zhu - SJTU  Friday, February 02, 2007 4:08 AM

Add two events as follow:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

if (e.ColumnIndex == 0)

{

string a = this.dataGridView1.CurrentCell.Value.ToString();

MessageBox.Show(a);

}

}

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

hope it help you

Bob zhu - SJTU  Friday, February 02, 2007 4:47 AM
Thanks, it works but may i know what does the following code mean? Thank you.

Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged

DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

End Sub

lucerias  Friday, February 02, 2007 6:05 AM

Hi:

this one make the datagidview accept changes form checked box directlly when it received info,

I think without this code, actually the value of DGV cell wouldn't until validtion event coming 

and by the way, your tool convert c# to VB is so cool:)

Best regards!

Bob zhu - SJTU  Friday, February 02, 2007 7:12 AM

You can use google to search for other answers

Custom Search

More Threads

• Bound DataGridView behavior for new rows
• Listbox XML file databining
• Trouble with owner-draw for ListBox control
• Columns Shown From DataSet
• Any controls like Sybase's datawindow?
• Not saving data when read in via a foreign key in SQL
• Please, approve the bug!
• ContextSwitchDeadlock was detected on DataAdapter.Fill();
• rebind datagrid in Visual C# 2003
• datagrid combobox