|
-
Mar 14th, 2005, 07:45 AM
#1
Thread Starter
Member
Datagrid & Checkboxs [Resolved]
Hello, this is what I want to do...
I'have got a value in my database that can be true or false, en when I read out my table into a datagrid, I want a checkbox that is checked when the value is true and unchecked when it is false.
First it was a pain just getting the checkbox in the datagrid, but I got this solved by using a template Column.
But I realy haven't got a clue how to preset the checkbox ....
My code behind code
Code:
myCommand = myConn.CreateCommand();
myCommand.CommandText = "select * from tbl_firm";
OracleDataAdapter oda = new OracleDataAdapter(myCommand);
DataSet myDs = new DataSet("Firms");
myCommand.Transaction = myOracleTransaction;
oda.Fill(myDs);
myCommand.ExecuteNonQuery();
GridFirms.DataSource = myDs;
GridFirms.DataBind();
myOracleTransaction.Commit();
this.myConn.Close();
And this is my asp code :
Code:
<asp:datagrid id="GridFirms" runat="server" AutoGenerateColumns="False" GridLines="Vertical">
<AlternatingItemStyle BackColor="Silver"></AlternatingItemStyle>
<ItemStyle BackColor="DarkGray"></ItemStyle>
<HeaderStyle Font-Bold="True" BackColor="LightGray"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="FIRM_NAME" HeaderText="Firm">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FIRM_IS_OEM" HeaderText="OEM?">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FIRM_IS_TIER1" HeaderText="Tier1">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FIRM_IS_RECTICEL" HeaderText="Recticel?">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Test">
<ItemTemplate>
<asp:CheckBox id=test Runat=server></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Last edited by Draven Chen Zhen; Apr 15th, 2005 at 01:28 AM.
-
Mar 14th, 2005, 07:54 AM
#2
Re: Datagrid & Checkboxs
In your datagrid's ITEMDATABOUND event, do a check on the dataset. For the corresponding value, get the DataGridItemCollection and do a FindControl for your checkbox. Set its value accordingly.
-
Mar 14th, 2005, 09:17 AM
#3
Thread Starter
Member
Re: Datagrid & Checkboxs
 Originally Posted by mendhak
In your datagrid's ITEMDATABOUND event, do a check on the dataset. For the corresponding value, get the DataGridItemCollection and do a FindControl for your checkbox. Set its value accordingly.
I need more help
This is what I got so far ....
Code:
private void ItemDataBoundEvent(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataRowCollection myCol = myDs.Tables[0].Columns[0].Table.Rows;
for(int i = 0; i< myCol.Count ; i++)
{
Response.Write(i + " ");
DataRow myRow = myCol[i];
LabelError.Text = myRow[2].ToString();
}
}
So far I know what the value of my records are, now I only need to check (or not to check) the correct checkboxes...
ps this is the part I need help 
PS : when I search for the control , I can't seem to find it. My Checkbox id = test, this is what I do
Code:
Control myCheck = FindControl("test");
Response.Write(myCheck.ClientID);
Last edited by Draven Chen Zhen; Mar 14th, 2005 at 09:39 AM.
-
Mar 17th, 2005, 02:18 AM
#4
Thread Starter
Member
Re: Datagrid & Checkboxs
Nevermind, I fixed the problem
-
Mar 17th, 2005, 04:02 AM
#5
Re: Datagrid & Checkboxs
Mind sharing the solution for future searchers?
-
Mar 18th, 2005, 03:02 AM
#6
Thread Starter
Member
Re: Datagrid & Checkboxs
K First of all make your column in the datagird.
I used a template colomn but I've been told it also works with a boundcolumn
Code:
<asp:TemplateColumn HeaderText="OEM?">
<HeaderStyle HorizontalAlign="Center" Width="65px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="CheckOem" Enabled="False" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
So as you can see, I added and ItemTemplate to my templateColumn, in the ItemTemplate you can add a checkbox. So that was my first problem.. that is now solved .
Next up was to preset the checkbox. I read the value from a database and when it is true it has to check my new made checkbox.
so here we go
First I make a new DataGridItem, but it is empty.
DataGridItem temp1 = null;
next I look up and Item of my DataGrid and put it in my temp1
temp1 = GridFirms.Items[i]; (i = number of datagrid row).
Next you need to "find" your checkbox, so we are going to search in our collection (temp1) for a control.
CheckBox temp2 = (CheckBox)temp1.FindControl("CheckOem");
So I know it is a checkbox => we are parsing it to a checkbox and I know the id of the checkbox in the datagrid is named "CheckOem". There for I'm looking up the name "CheckOem".
So know we have put the checkbox in our new made control (type checkbox) so know we can do with it what we want
temp2.Checked = true;
This is my code
Code:
DataGridItem temp1 = null;
temp1 = GridFirms.Items[i];
CheckBox temp2 = (CheckBox)temp1.FindControl("CheckOem");
temp2.Checked = true;
This is the code in my project, here I read the values from a database, when true I preset 3 checkboxes
Code:
private void CheckBoxCheck()
{
common.Connect2db(myConn, LabelError);
try
{
DataRowCollection myCol = myDs.Tables[0].Columns[0].Table.Rows;
for(int i = 0; i< myCol.Count ; i++)
{
DataRow myRow = myCol[i];
//Checkbox : Is Oem?
if(myRow[3].ToString() == "True")
{
DataGridItem temp1 = null;
temp1 = GridFirms.Items[i];
CheckBox temp2 = (CheckBox)temp1.FindControl("CheckOem");
temp2.Checked = true;
}
//Checkbox : Is Recticel?
if(myRow[4].ToString() == "True")
{
DataGridItem temp1 = null;
int test = GridFirms.Items.Count;
temp1 = GridFirms.Items[i];
CheckBox temp2 = (CheckBox)temp1.FindControl("CheckRec");
temp2.Checked = true;
}
//Checkbox : Is Tier1?
if(myRow[5].ToString() == "True")
{
DataGridItem temp1 = null;
int test = GridFirms.Items.Count;
temp1 = GridFirms.Items[i];
CheckBox temp2 = (CheckBox)temp1.FindControl("CheckTier1");
temp2.Checked = true;
}
}
}
Peace!
(ps: sry for late reaction, was on holiday )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|