Important Points
- To wrap the Text in source Code of Visual Studio
Ctrl E + Ctrl W
2. To Format Ctrl A + Ctrl K + Ctrl D
3. To hide Tree View in Crystal Report :
CrystalReportViewer.DisplayGroupTree = false;
4. How to remove   from Gridview
Include the line ConvertEmptyStringToNull="true" HtmlEncode="false" HtmlEncodeFormatString="false" in theBound field
eg. <asp:BoundField DataField="Serial No" HeaderText="Serial No" ConvertEmptyStringToNull="true" HtmlEncode="false" HtmlEncodeFormatString="false" />
5. To remove rows of the datatable while loading to the Combobox
If the datatable contains 4 rows and u want only first two rows
eg .
Machine
Accesories
Spare
oil
Required only
Machine
Accesories
then,
dsAssetType.Tables[0].Rows.RemoveAt(3);
dsAssetType.Tables[0].Rows.RemoveAt(2);
Coding eg.
//--------Asset Type ComboBox----
MasAssetTypeBLL masAssetTypeBLL = new MasAssetTypeBLL();
DataSet dsAsset = new DataSet();
dsAsset = masAssetTypeBLL.SelectAssetTyp(0);// load the data to dataset
dsAsset.Tables[0].Rows.RemoveAt(3);// first remove row 3
dsAsset.Tables[0].Rows.RemoveAt(2); // remove row 2
ddlAssetType.DataSource = dsAsset.Tables[0];//load it to ddl
ddlAssetType.DataTextField = "AssetTypeName";
ddlAssetType.DataValueField = "ID";
ddlAssetType.DataBind();
5a. How to Delete a row from Grid
- Create function Datagridtodatatable() [Refer Ponit No. 8]
- Create Datatable globally (before pageload)
- Remove the e.RowIndex form the datatable
- Bind it to grid.
eg. Datagridtodatatable();
dttable.Rows.RemoveAt(e.RowIndex);
dtgGrid.DataSource = dttable;
dtgGrid .DataBind();
6.To bind the field in the Template of Datagrid(Grid View).
In aspx source add the following line
Text='<%# Bind("field")%>'
In text change event
TextBox txt = (TextBox)sender;
GridViewRow row = (GridViewRow)txt.NamingContainer;
int i = row.RowIndex;
TextBox month = ((TextBox)dtgMachineDetails.Rows[i].FindControl("txtMachineMonthly"));
eg.
<asp:TextBox ID="txtMachineMonthly" runat="server" ontextchanged="txtMachineMonthly_TextChanged" AutoPostBack="True" Text='<%# Bind("MonthlyCost")%>' Height="22px" Width="100px" CssClass="style5"></asp:TextBox>
protected void txtMachineMonthly_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
GridViewRow row = (GridViewRow)txt.NamingContainer;
int i = row.RowIndex;
TextBox month = ((TextBox)dtgMachineDetails.Rows[i].FindControl("txtMachineMonthly"));
}
7. To make text field readonly
if (!IsPostBack)
{
textname.Attributes.Add("readonly", "readonly");
}
8. How to transfer multiplerec from grid to datatable...Datagridtodatatable
- . Create Datatable
- Declare Datatable globally (before pageload)
- In for loop count no. of rows in grid
- Add it to Datatable
eg.
private void CreateDataTableACC()
{
dttable.Columns.Add("Accessories ID");
dttable .Columns.Add("Asset Code");
dttable .Columns.Add("Asset Name");
dttable .Columns.Add("Remarks");
}
private void DatagridTodatatable
{
CreateDataTableACC();
for (int i = 0; i < dtgAccessoriesDetails.Rows.Count; i++)
{
dttable .Rows.Add(
dtgAccessoriesDetails.DataKeys[i]["Accessories ID"],
dtgAccessoriesDetails.Rows[i].Cells[1].Text,
dtgAccessoriesDetails.Rows[i].Cells[2].Text,
dtgAccessoriesDetails.Rows[i].Cells[3].Text);
}
}
9. Button to Add record to Gridview using Datatable
- Create Datagridtodatatable [refer pointNo. 8]
- Create New Row
- Add row to table
- Bind it to grid
eg.
private void CreateDataTable()
{
dttable.Columns.Add("Accessories ID");
dttable .Columns.Add("Asset Code");
dttable .Columns.Add("Asset Name");
dttable .Columns.Add("Remarks");
}
private void DatagridTodatatable
{
CreateDataTable();
for (int i = 0; i < dtgAccessoriesDetails.Rows.Count; i++)
{
dttable .Rows.Add(
dtgAccessoriesDetails.DataKeys[i]["Accessories ID"],
dtgAccessoriesDetails.Rows[i].Cells[1].Text,
dtgAccessoriesDetails.Rows[i].Cells[2].Text,
dtgAccessoriesDetails.Rows[i].Cells[3].Text);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DatagridTodatatable ();// old records are transfered to datatable
DataRow dr = dttable .NewRow(); // new row created
dr["Accessories ID"] = lblAccessoryID.Text.Trim();
dr["Asset Code"] = txtAccessoryName.Text.Trim();
dr["Asset Name"] = txtAccessoryName1.Text.Trim();
dr["Remarks"] = txtAttachmentAccessoryRemarks.Text.Trim();
dtAssetAccessories.Rows.Add(dr); // add new row to datatable
dtAssetAccessories.AcceptChanges();
dtgAccessoriesDetails.DataSource = dtAssetAccessories; //bind to grid
dtgAccessoriesDetails.DataBind();
}
{
dttable.Columns.Add("Accessories ID");
dttable .Columns.Add("Asset Code");
dttable .Columns.Add("Asset Name");
dttable .Columns.Add("Remarks");
}
private void DatagridTodatatable
{
CreateDataTable();
for (int i = 0; i < dtgAccessoriesDetails.Rows.Count; i++)
{
dttable .Rows.Add(
dtgAccessoriesDetails.DataKeys[i]["Accessories ID"],
dtgAccessoriesDetails.Rows[i].Cells[1].Text,
dtgAccessoriesDetails.Rows[i].Cells[2].Text,
dtgAccessoriesDetails.Rows[i].Cells[3].Text);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DatagridTodatatable ();// old records are transfered to datatable
DataRow dr = dttable .NewRow(); // new row created
dr["Accessories ID"] = lblAccessoryID.Text.Trim();
dr["Asset Code"] = txtAccessoryName.Text.Trim();
dr["Asset Name"] = txtAccessoryName1.Text.Trim();
dr["Remarks"] = txtAttachmentAccessoryRemarks.Text.Trim();
dtAssetAccessories.Rows.Add(dr); // add new row to datatable
dtAssetAccessories.AcceptChanges();
dtgAccessoriesDetails.DataSource = dtAssetAccessories; //bind to grid
dtgAccessoriesDetails.DataBind();
}
DBCC CHECKIDENT(M_Type, RESEED, 0)
Comments
Post a Comment