Reputation: 120
I have Form1
with several private TextBoxes. I want to pass some values from my DataGridView in Form2
to those TextBoxes inForm1
(when I press Enter for example).
What I want to do is to pass the values of rows of current selected Row
in DataGridView to be passed to TextBoxes in Form1
:
(I know how to get the values of selected row in datagridview my question is just the title...)
if (e.KeyCode == Keys.Enter)
{
SqlCommand sqlcmd = new SqlCommand("SELECT ID FROM X WHERE ID=" +
dataGridView1.CurrentRow.Cells[0].Value + "", sqlcon);
SqlDataReader sqldr = sqlcmd.ExecuteReader();
while (sqldr.Read())
{
Form1.CodeTextBox = sqldr[codecolumn].Tostring
Form1.NameTextBox = sqldr[Namecolumn].Tostring
Form1.BlahTextBox = sqldr[Blahcolumn].Tostring
}
}
which yells at me:
codeTextBox is private... not able to do so because of protection level...
I think I have to make a static class to do so, but I dont know how.
I would appriciate if someone would explain me that.
Form1
Class:
public partial class Form1 : Form
{
public static string searchString;
SqlConnection sqlcon =
new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True");
public Form1()
{
InitializeComponent();
SqlDataAdapter sqlda = new SqlDataAdapter("GetInvoice", sqlcon);
DataTable dt = new DataTable();
sqlda.Fill(dt);
dataGridView1.DataSource = dt;
}
//Xbuton_click datagridview_blah ...
}
Upvotes: 0
Views: 98
Reputation: 356
try this
CodeTextBox text changed event
public void CodeTextBox_TextChanged(object sender, EventArgs e)
{
CodeTextBox.Text = ((TextBox)sender).Text;
}
SqlDataReader
while (sqldr.Read())
{
Form1 form1 = new Form1();
TextBox t = new TextBox();
t.Text=sqldr[codecolumn].Tostring;
form1.CodeTextBox_TextChanged(t,null);
}
Upvotes: 1
Reputation: 27
private string CodeTextBox; private string NameTextBox; private string BlahTextBox;
Public Form1(string CodeTextBox , string NameTextBox, string BlahTextBox)
{
this.CodeTextBox= CodeTextBox;
this.NameTextBox= NameTextBox;
this.BlahTextBox = BlahTextBox;
}
txtCodeTextBox.Text = CodeTextBox;
txtNameTextBox.Text = NameTextBox;
txtBlahTextBox.Text = BlahTextBox;
if (e.KeyCode == Keys.Enter)
{
SqlCommand sqlcmd = new SqlCommand("SELECT ID FROM X WHERE ID=" + dataGridView1.CurrentRow.Cells[0].Value + "", sqlcon);
SqlDataReader sqldr = sqlcmd.ExecuteReader();
while (sqldr.Read())
{
string CodeTextBox = sqldr[codecolumn].Tostring;
string NameTextBox = sqldr[Namecolumn].Tostring;
string BlahTextBox = sqldr[Blahcolumn].Tostring;
Form1 frm = new Form1(CodeTextBox, NameTextBox, BlahTextBox);
}
}
Upvotes: 2