No errors, but still the grid does not change, it still shows all the columns
at the default width.
This first block of code sets the data columns, the next block of code
enters values in the datagrid and changes the width of the columns so only 2
columns are seen on the user interface, and then clear the data out of the
grid. I add data and then clear the grid because the intial adding of data
to the grid takes a long time, so I add the intial data on load of the form,
use the user when entering data is not slowed down. The second block of code
does not seem to change the appearance of the datagrid.
private void txtLicYear_TextChanged(object sender, System.EventArgs e)
{
string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string sProc = "prGet_LicenseCode";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;
oCmd.Parameters.Add("@sLicenseYear", SqlDbType.NChar, 6);
oCmd.Parameters["@sLicenseYear"].Value = txtLicYear.Text;
SqlDataAdapter oDa = new SqlDataAdapter();
oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);
int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License Codes found for that year.", "No record
found", MessageBoxButtons.OK);
}
else
{
cboPrivilege.DataSource = ds.Tables[0];
cboPrivilege.DisplayMember = "LICENSE_CODE";
cboPrivilege.ValueMember = "LICENSE_CODE";
//cboPrivilege.SelectedIndex = -1;
DataTable dtGrid = ds.Tables[0].Clone();
dgPrivileges.DataSource = dtGrid;
}
}
}
}
private void frmDataEntry_Load(object sender, System.EventArgs e)
{
string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"];
MessageBox.Show(sConnString);
string sProc = "prGet_DefaultYear";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;
oCmd.Parameters.Add("@sDefaultYear", SqlDbType.NVarChar,
4);
oCmd.Parameters["@sDefaultYear"].Direction =
ParameterDirection.Output;
oCmd.ExecuteNonQuery();
oCn.Close();
txtLicYear.Text =
oCmd.Parameters["@sDefaultYear"].Value.ToString();
txtDateSold3.Text = txtLicYear.Text.Substring(2, 2);
txtTrip3.Text = txtLicYear.Text.Substring(2, 2);
cboAMPM.Text = "PM";
}
}
DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;
dt.Rows.Add(dr);
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "LicPrivilges";
DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200; //or directly use
colStyle.Width = 200;
colStyle.MappingName = dt.Columns[4].ColumnName;
DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200; //or directly use
colStyle2.Width = 200;
colStyle2.MappingName = dt.Columns[5].ColumnName;
dgPrivileges.TableStyles.Add(dgts);
dt.Clear();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
"Jeffrey Tan[MSFT]" said:
Hi Cadel,
Thanks for your feedback.
Oh, you get back to the original error in your code. You only added 2
DataGridTextBoxColumns into your dgts.GridColumnStyles, that is colStyle
and colStyle2. So you should use index 0 or 1 to refer these 2 column
styles, or you can directly use colStyle or colStyle2 references to
manipulate these 2 column style width.
Ok, I see that you must have some misunderstanding of the databinding of
Winform DataGrid. In our scenario, you only want to display 2 columns in UI
side, so we only created 2 DataGridTextBoxColumns, then add them into
DataGridTableStyle. So in DataGridTableStyle.GridColumnStyles property
collection, we can only use index 0 or 1(because there are only 2 column
styles, why do you use 3 and 4 as index?). However, these index 0 and 1
column style maps to index 4 and 5 DataColumn in the DataTable, so the UI
side and the DataTable side have different index to use. The modified code
listed below:
DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;
dt.Rows.Add(dr);
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "LicPrivilges";
DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200; //or directly use
colStyle.Width=200;
colStyle.MappingName = dt.Columns[4].ColumnName;
DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200;//or directly use
colStyle2.Width=200;
colStyle2.MappingName = dt.Columns[5].ColumnName;
dgPrivileges.TableStyles.Add(dgts);
dt.Clear();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
I hope this will be much clear now. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.