Tuesday, August 22, 2006

Datagrid with CheckBoxes in C# example

First create a datagrid with checkboxes in it:

asp:TemplateColumn
ItemTemplate
asp:CheckBox Runat="server" ID="chkReq" Text="Request"

/asp:CheckBox
/ItemTemplate
/asp:TemplateColumn


Second the code:


private void btnConfirm_Click(object sender, System.EventArgs e)
{
// check if there is not two selected in the whole grid
if (!CheckSelectionError() == true)
{
// Display Error Message
lblMessage.Text = "Error some row has both checkboxes selected";
return;
}
else
{
lblMessage.Text="All checks fine";
}
// if no process all rows row by row
SaveSelection();
}


Now the loop trhu all items in the grid and do stuff with them:

// function to save the selection to the DB
public bool SaveSelection()
{
// declare instance of LAR
NAA.DAL.LAR.Reader objLar = new NAA.DAL.LAR.Reader();
// declare variables
string strULN;
int intQualID;
string strQualDate;
string strCentreName;
string strEndorsement;
CheckBox chk = new CheckBox();
// check each datagrid item
foreach (DataGridItem i in dgQualifications.Items)
{
// find chkDec
chk = (CheckBox) i.FindControl ("chkDec");
// check if it is selected
if (chk.Checked == true)
{
// Decline this row
}
// find chkReq
chk = (CheckBox) i.FindControl ("chkReq");
// check if it is selected
if (chk.Checked == true)
{
// get the details
strULN = dgQualifications.Items[i].Cells[0].Text;
intQualID = dgQualifications.Items[I].Cells[2].Text;
strQualDate = dgQualifications.Items[1].Cells[3].Text;
strCentreName = dgQualifications.Items[1].Cells[4].Text;
strEndorsement= dgQualifications.Items[1].Cells[5].Text;
// Request this row
objLar.SavePossibleQualificationClaim(strULN, intQualID, strQualDate, strCentreName, strEndorsement);
//objLar.FlagPossibleClaimAsProcessed();
}
}
// if it got here all ok
return true;
}

Method to check if not two checkboxes on one row are selected:

// function to find out in not both checkboxes were checked
public bool CheckSelectionError()
{
CheckBox chk = new CheckBox();
int intNoOfChecked;
// check each datagrid item
foreach (DataGridItem i in dgQualifications.Items)
{
intNoOfChecked = 0;
// find chkDec
chk = (CheckBox) i.FindControl ("chkDec");
// check if it is selected
if (chk.Checked == true)
{
intNoOfChecked += 1;
}
// find chkReq
chk = (CheckBox) i.FindControl ("chkReq");
// check if it is selected
if (chk.Checked == true)
{
intNoOfChecked += 1;
}
// make a decision
if (intNoOfChecked == 2)
{
return false;
}
}
// if it got here all ok
return true;
}

Optionaly this could be done using two option boxes but there will be a need of Javascript to uncheck the other one when one is checked

1 comment:

Anonymous said...

Do You interesting of [b]Female use of Viagra[/b]? You can find below...
[size=10]>>>[url=http://listita.info/go.php?sid=1][b]Female use of Viagra[/b][/url]<<<[/size]

[URL=http://imgwebsearch.com/30269/link/buy%20viagra/1_valentine3.html][IMG]http://imgwebsearch.com/30269/img0/buy%20viagra/1_valentine3.png[/IMG][/URL]
[URL=http://imgwebsearch.com/30269/link/buy%20viagra/3_headsex1.html][IMG]http://imgwebsearch.com/30269/img0/buy%20viagra/3_headsex1.png[/IMG][/URL]
[b]Bonus Policy[/b]
Order 3 or more products and get free Regular Airmail shipping!
Free Regular Airmail shipping for orders starting with $200.00!

Free insurance (guaranteed reshipment if delivery failed) for orders starting with $300.00!
[b]Description[/b]

Generic Viagra (sildenafil citrate; brand names include: Aphrodil / Edegra / Erasmo / Penegra / Revatio / Supra / Zwagra) is an effective treatment for erectile dysfunction regardless of the cause or duration of the problem or the age of the patient.
Sildenafil Citrate is the active ingredient used to treat erectile dysfunction (impotence) in men. It can help men who have erectile dysfunction get and sustain an erection when they are sexually excited.
Generic Viagra is manufactured in accordance with World Health Organization standards and guidelines (WHO-GMP). Also you can find on our sites.
Generic [url=http://viagra.opuskali.ru]Viagra Super Active[/url] is made with thorough reverse engineering for the sildenafil citrate molecule - a totally different process of making sildenafil and its reaction. That is why it takes effect in 15 minutes compared to other drugs which take 30-40 minutes to take effect.
[b]Buy Viagra In Atlanta
Viagra Ointment
Viagra Over The Counter
pic viagra woman
comparison levitra viagra
Viagra Sales In 2007
generic viagra on sale
[/b]
Even in the most sexually liberated and self-satisfied of nations, many people still yearn to burn more, to feel ready for bedding no matter what the clock says and to desire their partner of 23 years as much as they did when their love was brand new.
The market is saturated with books on how to revive a flagging libido or spice up monotonous sex, and sex therapists say “lack of desire” is one of the most common complaints they hear from patients, particularly women.