This is a discussion on C#.Net, ASP.Net Data Grid Frequently Asked Questions within the C# Programming forums, part of the Software Development category; Hi All, Here is the space to discuss about data grid using ASP.Net , C#. Net in Web development and ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi sathish... Here is a solution . It uses reflection to access the protected DataGridRows collection so he can set the row height. public void AutoSizeGrid() { // DataGrid should be bound to a DataTable for this part to // work. int numRows = ((DataTable)gridTasks.DataSource).Rows.Count; Graphics g = Graphics.FromHwnd(gridTasks.Handle); StringFormat sf = new StringFormat(StringFormat.GenericTypographic); SizeF size; // Since DataGridRows[] is not exposed directly by the DataGrid // we use reflection to hack internally to it.. There is actually // a method get_DataGridRows that returns the collection of rows // that is what we are doing here, and casting it to a System.Array MethodInfo mi = gridTasks.GetType().GetMethod("get_DataGridRows", BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); System.Array dgra = (System.Array)mi.Invoke(gridTasks,null); // Convert this to an ArrayList, little bit easier to deal with // that way, plus we can strip out the newrow row. ArrayList DataGridRows = new ArrayList(); foreach (object dgrr in dgra) { if (dgrr.ToString().EndsWith("DataGridRelationshipRow ")==true) DataGridRows.Add(dgrr); } // Now loop through all the rows in the grid for (int i = 0; i < numRows; ++i) { // Here we are telling it that the column width is set to // 400.. so size will contain the Height it needs to be. size = g.MeasureString(gridTasks[i,1].ToString(),gridTasks.Font,400,sf); int h = Convert.ToInt32(size.Height); // Little extra cellpadding space h = h + 8; // Now we pick that row out of the DataGridRows[] Array // that we have and set it's Height property to what we // think it should be. PropertyInfo pi = DataGridRows[i].GetType().GetProperty("Height"); pi.SetValue(DataGridRows[i],h,null); // I have read here that after you set the Height in this manner that you should // Call the DataGrid Invalidate() method, but I haven't seen any prob with not calling it.. } g.Dispose(); } Thannx... |
| |||
| Hi Kirthika, The first one System.Web.UI.WebControls.DataGrid is the DataGrid control used in developing web application and the second one System.Windows.Forms.DataGrid is meant for Windows application programming. Note: All the controls which comes under System.Web namespace is meant for developing web application and the controls which comes under System.Windows namespace is meant for Windows application development
__________________ J.Saravanan |
| |||
| Hi all, Can Anybody tell me how to remove the duplicate rows in a dataset please. If possible give me sample code. Thanx in Advance... M. Ramesh kumar |
| |||
| Hi Ramesh, This method will remove the duplicate rows from a datatable, based on a column. Pass to the method the datatable and the column to be checked for duplicate values. Call the method like this : Code: RemoveDuplicateRows(dT,"ID"); Code: public void RemoveDuplicateRows(DataTable dTable,string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
foreach(DataRow drow in dTable.Rows)
{
try
{
hTable.Add(drow[colName],string.Empty);
}
catch
{
duplicateList.Add(drow);
}
}
foreach(DataRow dRow in duplicateList)
dTable.Rows.Remove(dRow);
} Sundaram |
| |||
| hi all, copy past this code and make change with ur app path etc... Code: cmd.CommandText = "delete from emp where id not in (select min(id) from emp group by name,address)"
cmd.ExecuteNonQuery()
da.SelectCommand = cmd
da.Fill(ds, "emp")
DataGrid1.DataSource = ds.Tables("emp")
DataGrid1.SetDataBinding(ds, "emp")
DataGrid1.Refresh() SathisKumar R
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE Last edited by Sathish Kumar : 12-27-2007 at 11:54 PM. |
| |||
| Hi guys, I must add custom ColumnStyles to the designer so I can use them in a DataGrid at design time.Can anybody tell me how to do this?
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi sathish... To use custom ColumnStyles in the designer, you need to do three things. 1) Derive a CustomColumnStyle to implement the functionality you want. 2) Derive a DataGridTableStyle class and add a new GridColumnStyles property that uses this derived CollectionEditor. This GridColumnStyle hides the base class member. 3) Derive a DataGrid and add a new TableStyles collection that uses your derived table style. Both steps 2 and 3 will require you to derive a CollectionEditor and override CreateNewItemTypes to use the derived classes from each step in the designer. Thnx... |
| |||
| Hi, I am losing the settings on a DataGrid set during design-time.Do anybody know why this happens?
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi Sathish... This is possible if you assign a custom DataGridTableStyle to the DataGrid. The properties in the DataGridTableStyle will then replace certain properties in the DataGrid. Take a look at DataGrid class reference for a list of these properties. Thnx.. |
| |||
| Hi.. The important thing to know about your question is that you can't add a row to a datagrid, you must add a row to the datasource for the grid then re-bind the grid. Usually, the datasource for the grid is some class that has a item count property. From that you can determine the last item index, then add 1, set the EditItemIndex of the grid to that value and re-bind the grid. Now you have a "new row" in the grid that is set up for data entry. Then you just need to modify your logic a bit to do a data insert instead of an update. When you save the row. Hope this will help you... |
| |||
| Hi... Here is the sample source code... ASP. Net Source Code: <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestDGrid.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 16px" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="Id"> <ItemTemplate> <asp:TextBox id="txtID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "iEid ") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Name"> <ItemTemplate> <asp:TextBox id="txtName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "cEName") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Salary"> <ItemTemplate> <asp:TextBox id="txtSal" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "dSalary") %>'> </asp:TextBox> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Button id="btAdd" style="Z-INDEX: 102; LEFT: 64px; POSITION: absolute; TOP: 24px" runat="server" Text="Add"></asp:Button> </form> </body> </HTML> Code: using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace TestDGrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DataTable table = new DataTable();
protected System.Web.UI.WebControls.Button btAdd;
SqlConnection con = new SqlConnection(@"Server=Servername;DataBase=database name;uid=sa;pwd=sa;");
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{ fill();
BindDG();
}
}
public void fill()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from emp",con);
DataSet ds = new DataSet();
da.Fill(table);
}
public void BindDG()
{
DataGrid1.DataSource=table;
DataGrid1.DataBind();
}
public void Nrow()
{
table.Rows.InsertAt(table.NewRow(),0);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btAdd.Click += new System.EventHandler(this.btAdd_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btAdd_Click(object sender, System.EventArgs e)
{
DataGrid1.CurrentPageIndex = 0;
fill();
Nrow();
BindDG();
}
}
} |
| |||
| Hi, The DataGrid class does not have a property that controls whether a new row can be added. But the DataView class does have such a property (along with some others such as AllowEdit and AllowDelete).
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi, Here is the sample code. Code: string connString = @"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\northwind.mdb";
string sqlString = "SELECT * FROM customers";
// Connection object
OleDbConnection connection = new OleDbConnection(connString);
// Create data adapter object
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlString, connection);
// Create a dataset object and fill with data using data adapter's Fill method
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "customers");
// Attach dataset's DefaultView to the datagrid control
dataGrid1.DataSource = dataSet.Tables["customers"];
//no adding of new rows thru dataview...
CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
((DataView)cm.List).AllowNew = false;
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| Hi, If your datagrid contains links, then I would suggest adding Navigate handler such as the one below to disallow the AddNew. Code: private void DataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
if(ne.Forward)
{
CurrencyManager cm = (CurrencyManager)BindingContext[DataGrid1.DataSource,DataGrid1.DataMember];
DataView dv = (DataView) cm.List;
dv.AllowNew = false;
}
}
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Paging in Grid | Kirubhananth | ASP and ASP.NET Programming | 8 | 11-28-2008 01:51 AM |