|
|||||||||||||
| Registrarse | FAQ/Ayuda | Posts de hoy | Tags | ||||||||||
|
| |||
| Tags: Ninguna | |||
|
|
|
LinkBack | Herramientas |
|
|
#1 |
|
Deviloper
Registrado: abril-2010
Posts: 84
![]() |
CREAR FILTRO EN GRIDVIEW (y pintar filas)
Saludos estimados, intenté buscar ayuda en el foro MSDN pero fue poco fructífero, tengo lo siguiente:
Quiero realizar un filtro en mi website que, según lo ingresado me pinte las filas de mi gridview, sean los caracteres que sean. Por ejemplo, ingreso "adm", le doy al botón BUSCAR y debería seleccionarme y pintarme las filas que contenga esas letras en cualquiera de sus campos. Lo ideal es que se ejecute en el evento CLICK de mi botón buscar. Tengo un texbox en dónde se ingresa la búsqueda, el botoón y el gridview en dónde se pintarian las filas y me carga los datos de mi base de datos. Cualquier ayuda sería bienvenida, estoy hace días con esto.!! Lo estoy realizando en c# + sqlserver. Acá el código de mi evento click del botoón BUSCAR. protected void btn_IngFormSearch_Click(object sender, EventArgs e) { foreach (GridViewRow row in gv_ListarCalibres.Rows) { for (int i = 1; i <= 11; i++) { TextBox txt = row.FindControl(string.Format("TextBox{0}", i)) as TextBox; //if ((txt != null) && (txt.Text == txt_IngFormBuscador.Text)) //e.row.BackColor = System.Drawing.Color.Red; } } } PD: Soy novato en c#, pero algo le aplico. |
|
|
|
|
|
#2 |
|
Usuario
Registrado: junio-2006
Ubicación: Santiago Centro
Posts: 331
![]() |
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)
yo lo que haría seria modificar el evento RowDataBound del Gridview, algo así:
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && TextBox_Busqueda.Text != "")
{
for (int x = 0; x < e.Row.Cells.Count; x++)
{
string stock = e.Row.Cells[x].Text;
if (stock.Contains(TextBox_Busqueda.Text))
{
e.Row.Style.Add("background-color", "#C69D9D");
}
}
}
}
|
|
|
|
|
|
#3 | |
|
Deviloper
Registrado: abril-2010
Posts: 84
![]() |
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)
Quote:
protected void btn_IngFormSearch_Click(object sender, EventArgs e) { foreach (GridViewRow row in gv_ListarCalibres.Rows) { for (int i = 1; i <= 11; i++) { TextBox txt = row.FindControl(string.Format("TextBox{0}", i)) as TextBox; if ((txt != null) && (txt.Text == txt_IngFormBuscador.Text)) { row.BackColor = System.Drawing.Color.Red; } } } } Se supone que creo una variable del tipo GRIDVIEWROW con la que voy iterando, luego, un bucle for que parte desde 1 hasta la cantidad de columnas de mi gridview, luego una variable del tipo TEXBOX la cuál será igual a los textbox de mis columnas plantillas con el formato "TextBox1, Textbox2".... etc, i si éstos están con algún dato y son iguales a lo que ingresé en mi txt_IngFormBuscador, debería pintarlos de rojo, ¿Verdad? pero no los PINTA!!! ![]() ![]() ![]() ![]() Pero, basado en tú sugerencia, ¿Cómo podría unir el botón BUSCAR con ese evento? porque se supone que debería apretar BUSCAR y ahí debería de pintar las filas con los resultados... ---------- Post added at 09:58 ---------- Previous post was at 09:53 ---------- Porsiaca, tengo mis columnas como plantilla ... uffff estoy hace días con ésto ---------- Post added at 10:11 ---------- Previous post was at 09:58 ---------- //__________________________________________________ __________________ //----------------- FUNCION BUSCADOR /FILTRO POR CARACTER------------ protected void btn_IngFormSearch_Click(object sender, EventArgs e) { foreach (GridViewRow row in gv_ListarCalibres.Rows) { for (int i = 1; i <= 11; i++) { TextBox txt = row.FindControl(string.Format("TextBox{0}", i)) as TextBox; if ((txt != null) && (txt.Text == txt_IngFormBuscador.Text)) { row.BackColor = System.Drawing.Color.Red; } } } } //__________________________________________________ __________________ //---------------- EVENTO ROWDATABOUND PINTAR FILAS ------------------ protected void gv_ListarCalibres_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && txt_IngFormBuscador.Text != "") { for (int x = 0; x < e.Row.Cells.Count; x++) { string stock = e.Row.Cells[x].Text; if (stock.Contains(txt_IngFormBuscador.Text)) { e.Row.Style.Add("background-color", "#C69D9D"); } } } } Pero mi pregunta ahora es, ¿Cómo podría desde el evento click del botoón BUSCAR invocar al evento ROWDATABOUND que me pintaría mis filas? |
|
|
|
|
|
|
#4 |
|
Usuario
Registrado: junio-2006
Ubicación: Santiago Centro
Posts: 331
![]() |
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)
En el evento RowDataBound puedes ver que control desencadeno en postback (en este caso seria el boton buscar)
How to determine which Control caused PostBack on ASP.NET page? Control que ha producido un evento PostBack | C# Determining the Control that Caused a PostBack Ahi filtras con un if si el control que hizo el postback es el botón buscar. Yo creo que como tu lo planteaste tienes el problema del orden en que se ejecutan los eventos (LOAD, CLICK , DATABIND, etc) hay un flujo determinado pero no me acuerdo como era. O en una de esas simplemente te falta un gv_ListarCalibres.DataBind() al final para actualizar el gridview |
|
|
|
|
|
#5 |
|
Deviloper
Registrado: abril-2010
Posts: 84
![]() |
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)
Como quiero hacer un filtro, y de tanto averiguar, ¿no sería más factible instansiar un DATAVIEW y desde ahí filtrar?
¿Qué me recomiendas? así me ahorraría la paja del RowDataBound, y mostraría los que coincida con el filtro en vez de pintarlos, al fin y al cabo, la misma funcionalidad pero menos bonita. ¿Qué opinas?. |
|
|
|
|
|
#6 | |
|
Usuario
Registrado: junio-2006
Ubicación: Santiago Centro
Posts: 331
![]() |
Re: CREAR FILTRO EN GRIDVIEW (y pintar filas)
Quote:
|
|
|
|
|
|
| Herramientas | |
|
|
|||||