Column Swap in Gridview

This is kind of a silly way of doing this but if you are somewhat UI bound this could be useful.

public void SwapColumns(GridView gridView,Int32 rowZero,Int32 rowOne)
{
for (int __rowIndexUno = 0; __rowIndexUno <= gridView.Rows.Count – 1; __rowIndexUno++)
{
GridViewRow row = gridView.Rows[__rowIndexUno];
for (int cellIndex = 0; cellIndex <= row.Cells.Count – 1; cellIndex++)
{
if (__rowIndexUno == 0)
{
string hCol0 = null;
string hCol1 = null;
hCol0 = gridView.HeaderRow.Cells[rowZero].Text;
hCol1 = gridView.HeaderRow.Cells[rowOne].Text;
gridView.HeaderRow.Cells[rowZero].Text = hCol1;
gridView.HeaderRow.Cells[rowOne].Text = hCol0;
}

if (cellIndex < 1 & row.Cells[rowZero].Visible)
{
string temp0 = row.Cells[rowZero].Text;
string temp1 = row.Cells[rowOne].Text;
row.Cells[rowZero].Text = temp1;
row.Cells[rowOne].Text = temp0;
}

}
}
}

protected void Button1_Click(object sender, EventArgs e)
{
SwapColumns(this.GridView1, System.Convert.ToInt32(DropDownList1.SelectedItem.Value), System.Convert.ToInt32( DropDownList2.SelectedItem.Value));
}

 

Flip

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s