DataGrid Learning Seven
Last Update:2017-02-28
Source: Internet
Author: User
The BoundColumn control in the DataGrid case is not the only control that can be set in the Columns collection of the DataGrid. You can also specify TemplateColumn, which gives you full control over the contents of the column. The contents of the template can be arbitrary, and any content, including server controls, can be rendered in the columns of the DataGrid. The following example shows how to use the TemplateColumn control to render a "state" column as a drop-down list and to render the "Contract" column as a check box HTMLControl. asp.net data-binding syntax is used to output data field values in a template. Note that there are some tricky logic to make Drop-down lists and check boxes reflect the data state in rows.
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script language= "C #" runat= "Server" >
SqlConnection myconnection;
Public Hashtable Stateindex;
protected void Page_Load (Object Src, EventArgs E)
{
myconnection = new SqlConnection ("User id=sa;password=;initial catalog=pubs;data Source=jeff");
if (! IsPostBack)
Bindgrid ();
Stateindex = new Hashtable ();
stateindex["CA"] = 0;
Stateindex["in"] = 1;
stateindex["KS"] = 2;
stateindex["MD"] = 3;
stateindex["MI"] = 4;
stateindex["OR"] = 5;
stateindex["TN"] = 6;
stateindex["UT"] = 7;
}
public int Getstateindex (String statename)
{
if (Stateindex[statename]!= null)
return (int) stateindex[statename];
Else
return 0;
}
public void Mydatagrid_edit (Object sender, DataGridCommandEventArgs E)
{
Mydatagrid.edititemindex = (int) E.item.itemindex;
Bindgrid ();
}
public void Mydatagrid_cancel (Object sender, DataGridCommandEventArgs E)
{
Mydatagrid.edititemindex =-1;
Bindgrid ();
}
public void Mydatagrid_update (Object sender, DataGridCommandEventArgs E)
{
String updatecmd = "UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone,"
+ "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";
SqlCommand mycommand = new SqlCommand (Updatecmd, MyConnection);
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Id", SqlDbType.NVarChar, 11));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@LName", SqlDbType.NVarChar, 40));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@FName", SqlDbType.NVarChar, 20));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Phone", SqlDbType.NChar, 12));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Address", SqlDbType.NVarChar, 40));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@City", SqlDbType.NVarChar, 20));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@State", SqlDbType.NChar, 2));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Zip", SqlDbType.NChar, 5));
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Contract", sqldbtype.nvarchar,1));
mycommand.parameters["@Id"]. Value = mydatagrid.datakeys[(int) e.item.itemindex];
String[] cols = {"LName", "FName", "Phone", "Address", "City", "Zip"};
for (int i=0; i<6; i++)
{
String Colvalue = ((TextBox) E.item.findcontrol ("Edit_" + cols[i)). Text;
Check to see if there is a null value in the desired field
if (i<3 && colvalue = "")
{
message.innerhtml = "Error: Name" or "phone" is not allowed to use null value ";
message.style["COLOR"] = "red";
Return
}
mycommand.parameters["@" + cols[i]]. Value = Colvalue;
}
mycommand.parameters["@State"]. Value = ((DropDownList) E.item.findcontrol ("Edit_state")). Selecteditem.tostring ();
if ((CheckBox) E.item.findcontrol ("Edit_contract"). Checked = = True)
mycommand.parameters["@Contract"]. Value = "1";
Else
mycommand.parameters["@Contract"]. Value = "0";
MyCommand.Connection.Open ();
Try
{
Mycommand.executenonquery ();
message.innerhtml = "<b> Updated record </b><br>" + updatecmd;
Mydatagrid.edititemindex =-1;
}
catch (SqlException e)
{
if (E.number = 2627)
message.innerhtml = "Error: Record with same primary key already exists";
Else
message.innerhtml = "Error: Failed to update record, make sure fields are filled in correctly";
message.style["COLOR"] = "red";
}
MyCommand.Connection.Close ();
Bindgrid ();
}
public void Bindgrid ()
{
SqlDataAdapter mycommand = new SqlDataAdapter ("select * from Authors", MyConnection);
DataSet ds = new DataSet ();
Mycommand.fill (ds, "Authors");
Mydatagrid.datasource=ds. tables["Authors"]. DefaultView;
Mydatagrid.databind ();
}
</script>
<body style= "font:10.5pt song body" >
<form runat= "Server" >
<span id= "message" enableviewstate= "false" style= "font:arial 11pt;" runat= "Server"/><p>
<asp:datagrid id= "Mydatagrid" runat= "Server"
Width= "800"
Backcolor= "#ccccff"
Bordercolor= "BLACK"
Showfooter= "false"
Cellpadding=3
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Oneditcommand= "Mydatagrid_edit"
Oncancelcommand= "Mydatagrid_cancel"
Onupdatecommand= "Mydatagrid_update"
Datakeyfield= "au_id"
Autogeneratecolumns= "false"
>
<Columns>
<asp:editcommandcolumn edittext= "edit" canceltext= "Cancel" updatetext= "Update" itemstyle-wrap= "false"/>
<asp:boundcolumn headertext= "au_id" sortexpression= "au_id" True "readonly=" datafield= "au_id" False "/>
<asp:templatecolumn headertext= "au_lname" sortexpression= "au_lname" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," au_lname ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" id= "Edit_lname" text= "<%# DataBinder.Eval (Container.DataItem," au_lname ")%>"/ >
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "au_fname" sortexpression= "au_fname" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," au_fname ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" id= "Edit_fname" text= "<%# DataBinder.Eval (Container.DataItem," au_fname ")%>"/ >
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Phone" sortexpression= "Phone" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," phone ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" id= "Edit_phone" text= "<%# DataBinder.Eval (Container.DataItem," Phone ")%>"/> "
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Address" sortexpression= "Address" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," address ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" id= "edit_address" text= "<%# DataBinder.Eval (Container.DataItem," address ")%>"/ >
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "City" sortexpression= "City" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," City ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" id= "edit_city" text= "<%# DataBinder.Eval (Container.DataItem," City ")%>"/> "
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "state" sortexpression= "state" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," state ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist runat= "Server" selectedindex= "<%# Getstateindex (DataBinder.Eval (Container.DataItem," state "). ToString ())%> "id=" Edit_state ">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "zip" sortexpression= "zip" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," zip ")%>"/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox runat= "Server" id= "Edit_zip" text= "<%# DataBinder.Eval (Container.DataItem," Zip ")%>"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "contract" sortexpression= "contract" >
<ItemTemplate>
<asp:label runat= "Server" text= "<%# DataBinder.Eval (Container.DataItem," contract "," {0} ")%>"/> "
</ItemTemplate>
<EditItemTemplate>
<asp:checkbox runat= "Server" id= "Edit_contract" checked= "<%# DataBinder.Eval (Container.DataItem," Contract ")% > "/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASP:DataGrid>
</form>
</body>