Friday, December 14, 2012

.NET basics 2


--fill grid

public DataSet getAllJobs()
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}

SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "select jobID, jobName, expirationDate, postedDate, jobCatName, subCategoryName from dbo.itpJobs J, dbo.itpJobCategory C, dbo.itpJobSubCategory S where J.jobCategory = C.jobCatId and J.jobSubCategory = S.jobSubCatid";

SqlDataAdapter da = new SqlDataAdapter(newCmd);
DataSet ds = new DataSet();
da.Fill(ds, "itpJobs");

conn.Close();

return ds;
}

--row header mouse click event

private void dgvJobs_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
txtJobName.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_jobName"].Value.ToString();

dtpExpiryDate.Value = (DateTime)dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_expirationDate"].Value;
           
lblJobID.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_jobId"].Value.ToString();

cmbJobCat.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_jobCat"].Value.ToString();
           
cmbSubCat.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_subCat"].Value.ToString();

      btnUpdate.Enabled = true;
     btnAdd.Enabled = false;           
}


--passing information between forms

public partial class JobDetails : Form
{
private string job_ID;
private string job_Name;

public JobDetails()
{
InitializeComponent();
}

public JobDetails(string jobId, string jobName)
{
InitializeComponent();
this.job_ID = jobId;
            this.job_Name = jobName;
}

private void JobDetails_Load(object sender, EventArgs e)
{
txtJobId.Text = job_ID;
txtJobName.Text = job_Name;
}























2 comments: