Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts
0

Double Click event for a Row in a Grid View

1. First while on OnRowDataBound Method of your GridView insert the following:



if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < (e.Row.Cells.Count); i++)
{
e.Row.Attributes.Add("ondblclick", "Javascript:rowDblClick();");
}
}
2. Write a javascript method in your aspx page.

  function rowDblClick(){ 
        alert("done!");
      }
 Thats it you are done !
0

GridView Row Color Change Using CSS

First define the css which you want to use. This you could do by defining a seperate css file or by adding the css in the same file. I have declared the css in the same file. The head section of my .aspx file looks like below.

<head runat="server">
    <title>Untitled Page</title>
    <style>
     .RowCss tr:hover
    {
        background-color:Orange;
    }
    </style>
</head>

Next you have to give this css class to the Grid View where you need to change your row.

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
 DataSourceID="SqlDataSource1" CssClass="RowCss" >

Expected Output:
0

Session Management For Grid View Control

I recently came across a situation, where I had to use session management for GRID VIEW CONTROL.


I had a search page where there was a grid displaying some data. Whenever a search was made, the data was fetched from the back end and displayed in the grid view. Now whenever the user navigates to another page and comes back to the search page, my gridview values should be still there. 
Use the code below in the appropriate place. 

// Store GridView in Session
Session["Test"] = GridView1;
GridView1 = null;


// Retrieve GridView from Session
GridView1 = (GridView)Session["Test"];


Hope this helps!
4

Add Row / Columns to GridView (Programmatically)


1) Create a DataTable object to which than we will bind the GridView

DataTable dt = new DataTable();
2) IF you need two columns than create two DataColumn objects

DataColumn dc1 = new DataColumn("first", typeof(string));

DataColumn dc2 = new DataColumn("second", typeof(string));
DataColumn dc3 = new DataColumn("third", typeof(string));

Add it to the table

dt.Columns.Add(dc1);

dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
3)Run the loop for as many rows you want to add. eg: 3 rows to be added.

for(int i=0;i<3;i++)// 3 = number of rows needed.

{
   DataRow row1 = dt.NewRow();
   int c = 0;
   while (c < 3)
   {
       row1[c] = "some_data";
       c++;
   }
   dt.Rows.Add(row1 );
}

Now iterate through each datacolumn and create a BoundField foreach column

foreach (DataColumn col in dt.Columns)

{
BoundField bField = new BoundField();
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}

GridView1.DataSource = dt;
//Bind the datatable with the GridView.
GridView1.DataBind();
And you have successfully added rows/cols to the grid !
 

2011 ·Code-Studio by yrus.