DataGridView

Is rhere anybody can explane how can I manipulate with data in datagridview
I have 4 textbox and when i press number iin text box1 must show me same data in textbox1,2,3 and in datagridview!!!

[199 byte] By [Gonza981] at [2008-1-9]
# 1

Sample Code:

Code Snippet

private void textBox1_TextChanged(object sender, EventArgs e)

{

this.textBox2.Text = this.textBox1.Text;

this.textBox3.Text = this.textBox1.Text;

this.textBox4.Text = this.textBox1.Text;

DataTable dt = new DataTable();

object[] obj=new object[1];

obj.SetValue(this.textBox1.Text,0);

//Add column and row

dt.Columns.Add();

dt.Rows.Add(obj);

if (dt.Rows.Count > 0)

{

this.dataGridView1.DataSource = dt;

}

}

Vidhura at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

How can I manipulate rows and columns.
I have one datagridview2 on form2 and when I press enter on rows they must show in datagridview1 on form1!
Vb 2005!!!!

Gonza981 at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Hi Gonza981,

Try to bind the dataGridView1 and dataGridView2 to the same data source. You may need a public class with a static DataTable in it. And the bind both datagridview to this data table.

Code Snippet

public class Data

{

public static DataTable datatable = new DataTable();

static Data()

{//generate initial data

datatable.Columns.Add("aa");

datatable.Columns.Add("bb");

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

{

datatable.Rows.Add("aa" + i, "bb" + i);

}

}

}

In Form1, bind datagridview to data table using the following code:

Code Snippet
this.dataGridView1.DataSource = Data.datatable;

In Form2, bind datagridview to data table using the following code:

Code Snippet
this.dataGridView2.DataSource = Data.datatable;

Hope this helps.
Best regards.

Rong-ChunZhang-MSFT at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4


Dear Rong-Chun Zhang,

I need Visual Basic 2005 code, I think you write C# code!

Gonza981 at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

Hi Gonza981,

Sorry, and here is the VB.Net code.

Code Snippet

Public Class Data

Public Shared Sharedatatable As New DataTable

Shared Sub New()

'generate initial data

Sharedatatable.Columns.Add("aa")

Sharedatatable.Columns.Add("bb")

For i As Integer = 0 To 49

Sharedatatable.Rows.Add("aa" + i.ToString(), "bb" + i.ToString())

Next i

End Sub

End Class

In Form1, bind datagridview to data table using the following code:

Code Snippet
Me.DataGridView1.DataSource = Data.Sharedatatable

In Form2, bind datagridview to data table using the following code:

Code Snippet
Me.DataGridView2.DataSource = Data.Sharedatatable

Hope this helps.
Best regards.

Rong-ChunZhang-MSFT at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

Dear Rong-Chun Zhang,
I also need when I press line of rows with enter in datagridview2 that line show in dtagridview1!!
Do you have code for exit from application,restart computer,shut down!!!

Gonza981 at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

Hi Gonza981,

What did you mean by 'when I press line of rows with enter in datagridview2 that line show in dtagridview1'? Did you mean synch the two DataGridView? If so, try to set the two form's BindingContext to the a same object. Your Form1's code may be something like the following. Hope this helps

Code Snippet

Public Class FormA

Dim bc As BindingContext = New BindingContext()

Private Sub FormA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.DataGridView1.DataSource = Data.Sharedatatable

Me.BindingContext = bc

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim f As New FormB()

f.Show()

f.BindingContext = bc

End Sub

End Class

In this thread, we are mainly discussing about "DataGridView" as indicated by the posts above and the title.

Since your new question "Do you have code for exit from application,restart computer,shut down" is not directly related to the original issue, it would be best if you open up a new thread for the new question. In this way, our discussion here will not deviate too much from the original issue. This will make answer searching in the forum easier and be beneficial to other community members as well.

Thank you for your understanding.

Rong-ChunZhang-MSFT at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8

I mean when I mark 2,3,5,6 ....n rows and with enter transport to grid1

Gonza981 at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9

Can I Give you VB6 code and you can see How can I Translate to VB 2005!

Gonza981 at 2007-10-2 > top of Msdn Tech,Windows Forms,Windows Forms General...