Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, August 25, 2010

Small things make me happy

    Today as I was coding, I was writing a regular "if-else" and decided to look at msdn for something different.


So, I found this cool way of writing simple "if-else"


  string[] coding = getVal.Text.Split(getVal.Text.Contains("-") ? '-' : ' ');





 Very elegant and very simple to understand. I love it !! 

Wednesday, October 3, 2007

Creating subheader in gridview


<asp:gridview id="GridView1" class="attr"> runat="server" autogeneratecolumns= class="attrv">"false" datasourceid="SqlDataSource1"
onrowdatabound="GridView1_RowDataBound">
<columns>
<asp:boundfield datafield= class="attrv">"ProductID" headertext="ProductID" />
<asp:boundfield datafield= class="attrv">"Name" headertext="Name" />
<asp:boundfield datafield= class="attrv">"Subcategory" headertext="Subcategory" />
</columns>
</asp:gridview>
<asp:sqldatasource id= class="attrv">"SqlDataSource1" runat="server" class="attr"> connectionstring="<%$
ConnectionStrings:AdventureWorksConnectionString %>"

selectcommand="SELECT P.*, PS.[Name]
AS [Subcategory] FROM [Production].[Product] AS P INNER JOIN [Production].[ProductSubcategory]
AS PS ON PS.[ProductSubcategoryID] = P.[ProductSubcategoryID] ORDER BY P.[ReorderPoint],
P.[Name]"
>
</asp:sqldatasource>


CODE-BEHIND 



private string m_subcategory = String.Empty;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;

switch (gvr.RowType)
{
case DataControlRowType.DataRow:
{
DataRowView drv = gvr.DataItem as DataRowView;

string subcategory = drv["subcategory"].ToString();

if (!subcategory.Equals(m_subcategory))
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.BackColor = Color.Red;
cell.ForeColor = Color.White;
cell.ColumnSpan = GridView1.Columns.Count;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Text = subcategory;
row.Cells.Add(cell);

GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count - 1, row);

m_subcategory = subcategory;
}


break;
}
}
}

Wednesday, May 16, 2007

How do I cast a string to an int, float, double, etc?

How do I cast a string to an int, float, double, etc?

You can use the Convert class or the Parse method of the built-in type you are casting to. i.e.

string myStr = "123";
int myParsedInt = Int32.Parse(myStr);
int myConvertedInt = Convert.ToInt32(myStr);

This example uses the int type, but you can also use the same techniques for any of the other integral or floating point types.