EDN Admin
Well-known member
<pre>I am trying to pass a textbox value(SQL select statement) to class that returns a list in a propertygrid. [/code]
<pre>I am relatively new to this so I am not sure how to do this. I can make the button return a value that is passed through the class back to the form. What I cant do is pass the value into the SQLString in the ClientType. In summary I trying to pass the textbox value to the SQLString in the class so it will return value in the propertygrid.[/code]
<pre>Any help would be greatly appreciated. [/code]
<pre>Thanks JDude[/code]
<pre> [/code]
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Reflection;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public class myPropZ
{
public class mOC
{
public string Val;
public mOC(string i)
{
Val = i;
}
public string Stuff
{
get { return this.Val; }
set { this.Val = value; }
}
}
// public string SQLString = "select distinct ClientType from WeightZ";
public string cnStr = SQLConString;
string myStrZ2;
[TypeConverter(typeof(MyConverterZ2))]
[CategoryAttribute("ClientTypes"), DescriptionAttribute("Client Type")]
public string ClientType
{
get { return myStrZ2; }
set { myStrZ2 = value; }
}
List<string> listZ2;
[Browsable(false)]
public List<string> MyListZ2
{
get
{
if (listZ2 == null)
{
listZ2 = new List<string>();
using (SqlConnection connection = new SqlConnection())
{
using (SqlCommand command = new SqlCommand())
{
connection.ConnectionString = cnStr
command.CommandType = CommandType.Text;
command.CommandText = SQLString;
command.Connection = connection;
connection.Open();
SqlDataReader myReader = command.ExecuteReader();
while (myReader.Read())
{
listZ2.Add(String.Format("{0}", myReader[0]));
}
myReader.Close();
connection.Close();
}
}
}
return listZ2;
}
}
}
class MyConverterZ2 : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
List<string> listZ2 = (context.Instance as Form1.myPropZ).MyListZ2;
StandardValuesCollection cols = new
StandardValuesCollection(listZ2);
return cols;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "select distinct ClientType from WeightZ";
myPropZ.mOC C = new myPropZ.mOC(textBox1.Text);
MessageBox.Show(C.Stuff.ToString());
myPropZ Client = new myPropZ();
Client.ClientType = "GX";
this.propertyGrid1.SelectedObject = Client;
}
}
}
[/code]
<br/>
View the full article
<pre>I am relatively new to this so I am not sure how to do this. I can make the button return a value that is passed through the class back to the form. What I cant do is pass the value into the SQLString in the ClientType. In summary I trying to pass the textbox value to the SQLString in the class so it will return value in the propertygrid.[/code]
<pre>Any help would be greatly appreciated. [/code]
<pre>Thanks JDude[/code]
<pre> [/code]
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Reflection;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public class myPropZ
{
public class mOC
{
public string Val;
public mOC(string i)
{
Val = i;
}
public string Stuff
{
get { return this.Val; }
set { this.Val = value; }
}
}
// public string SQLString = "select distinct ClientType from WeightZ";
public string cnStr = SQLConString;
string myStrZ2;
[TypeConverter(typeof(MyConverterZ2))]
[CategoryAttribute("ClientTypes"), DescriptionAttribute("Client Type")]
public string ClientType
{
get { return myStrZ2; }
set { myStrZ2 = value; }
}
List<string> listZ2;
[Browsable(false)]
public List<string> MyListZ2
{
get
{
if (listZ2 == null)
{
listZ2 = new List<string>();
using (SqlConnection connection = new SqlConnection())
{
using (SqlCommand command = new SqlCommand())
{
connection.ConnectionString = cnStr
command.CommandType = CommandType.Text;
command.CommandText = SQLString;
command.Connection = connection;
connection.Open();
SqlDataReader myReader = command.ExecuteReader();
while (myReader.Read())
{
listZ2.Add(String.Format("{0}", myReader[0]));
}
myReader.Close();
connection.Close();
}
}
}
return listZ2;
}
}
}
class MyConverterZ2 : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
List<string> listZ2 = (context.Instance as Form1.myPropZ).MyListZ2;
StandardValuesCollection cols = new
StandardValuesCollection(listZ2);
return cols;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "select distinct ClientType from WeightZ";
myPropZ.mOC C = new myPropZ.mOC(textBox1.Text);
MessageBox.Show(C.Stuff.ToString());
myPropZ Client = new myPropZ();
Client.ClientType = "GX";
this.propertyGrid1.SelectedObject = Client;
}
}
}
[/code]
<br/>
View the full article