2
Answers

Please Check My Coding

Ask a question
Wong Jun

Wong Jun

12y
1.3k
1
here is my sourcecode..
i did the highlight row with a condition.
it works but there is one problem. After it highlighted the 3rd row of the datagridview, the highlight stayed permanently at 3rd rows regardless of sorting or search function.here is my code

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.SqlTypes;
using System.Configuration;
using System.Windows.Forms.VisualStyles;

namespace CMSystem
{
public partial class MedicineInventory : Form
{
SqlDataAdapter da;
DataSet ds;
DataView dv;
DataTable dt;

int scrollValue;
public MedicineInventory()
{
InitializeComponent();

}



private void MedicineInventory_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cMSdatabaseDataSet.MedicineInventory' table. You can move, or remove it, as needed.
this.medicineInventoryTableAdapter.Fill(this.cMSdatabaseDataSet.MedicineInventory);


binddata();

}


private void binddata()
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSystem.Properties.Settings.CMSdatabaseConnectionString"].ConnectionString);
string SelectSQL = "Select * From MedicineInventory";
da = new SqlDataAdapter(SelectSQL, conn);
ds = new DataSet();
conn.Open();
da.Fill(ds, scrollValue, 10, "MedicineInventory");
conn.Close();
dataGridViewMedicine.DataSource = ds;

dataGridViewMedicine.DataMember = "MedicineInventory";

}

private void buttonExpiryDate_Click(object sender, EventArgs e)
{
int intCount;
DateTime.Today.ToString("dd-MM-yyyy");
intCount = ds.Tables["MedicineInventory"].Rows.Count;
binddata();
// this.dataGridViewMedicine.Sort(this.dataGridViewMedicine.Columns[3], ListSortDirection.Descending);
for (int i = 0; i < intCount; i++)
{
if (Convert.ToDateTime(ds.Tables[0].Rows[i]["Expiry"]) <= DateTime.Today)
{
dataGridViewMedicine.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
}
}
}


private void buttonCheckStocks_Click(object sender, EventArgs e)
{

int intCount;
intCount = ds.Tables["MedicineInventory"].Rows.Count;

for (int i = 0; i < intCount; i++)
{
if (Convert.ToInt32(ds.Tables[0].Rows[i]["Stocks"]) < 100)
{
dataGridViewMedicine.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}

private void buttonRefresh_Click(object sender, EventArgs e)
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSystem.Properties.Settings.CMSdatabaseConnectionString"].ConnectionString);
string SelectSQL = "Select * From MedicineInventory";
da = new SqlDataAdapter(SelectSQL, conn);
ds = new DataSet();
conn.Open();
da.Fill(ds, scrollValue, 10, "MedicineInventory");
conn.Close();
dataGridViewMedicine.DataSource = ds;
dataGridViewMedicine.DataMember = "MedicineInventory";

}

private void buttonNext_Click(object sender, EventArgs e)
{

scrollValue = scrollValue + 10;
if (scrollValue > 23)
{
scrollValue = 18;
}
ds.Clear();
da.Fill(ds, scrollValue, 10, "MedicineInventory");

}

private void buttonPrevious_Click(object sender, EventArgs e)
{
scrollValue = scrollValue - 10;
if (scrollValue <= 0)
{
scrollValue = 0;
}
ds.Clear();
da.Fill(ds, scrollValue, 10, "MedicineInventory");
}


private void SearchTxtBox_TextChanged(object sender, EventArgs e)
{
DataView dv = new DataView();

dv.Table = cMSdatabaseDataSet.MedicineInventory;
dv.RowFilter = "MedicineName like '" + this.SearchTxtBox.Text + "%'";
dataGridViewMedicine.DataSource = dv;

}



private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void medicineModificationToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmMedicine MediForm = new FrmMedicine();
MediForm.Show();
}

private void CheckInventoryButton_Click(object sender, EventArgs e)
{
int intCount;
DateTime.Today.ToString("dd-MM-yyyy");
intCount = ds.Tables["MedicineInventory"].Rows.Count;



for (int i = 0; i < intCount; i++)
{

if ((Convert.ToDateTime(ds.Tables[0].Rows[i]["Expiry"]) <= DateTime.Today) && (Convert.ToInt32(ds.Tables[0].Rows[i]["Stocks"]) < 100))
{



dataGridViewMedicine.Rows[i].DefaultCellStyle.BackColor = Color.LightSkyBlue;

}

}

}








}
}


Attachment: cmsystem 2.zip

Answers (2)